Skip to content

Commit

Permalink
store the server port as an int, not a string, in HTTP tests (#3959)
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Aug 16, 2023
1 parent 2e7ea91 commit 4f69656
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions integrationtests/self/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("HTTP tests", func() {
rt *http3.RoundTripper
server *http3.Server
stoppedServing chan struct{}
port string
port int
)

BeforeEach(func() {
Expand Down Expand Up @@ -93,7 +93,7 @@ var _ = Describe("HTTP tests", func() {
Expect(err).NotTo(HaveOccurred())
conn, err := net.ListenUDP("udp", addr)
Expect(err).NotTo(HaveOccurred())
port = strconv.Itoa(conn.LocalAddr().(*net.UDPAddr).Port)
port = conn.LocalAddr().(*net.UDPAddr).Port

stoppedServing = make(chan struct{})

Expand All @@ -120,7 +120,7 @@ var _ = Describe("HTTP tests", func() {
})

It("downloads a hello", func() {
resp, err := client.Get("https://localhost:" + port + "/hello")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/hello", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
body, err := io.ReadAll(gbytes.TimeoutReader(resp.Body, 3*time.Second))
Expand All @@ -129,12 +129,12 @@ var _ = Describe("HTTP tests", func() {
})

It("requests to different servers with the same udpconn", func() {
resp, err := client.Get("https://localhost:" + port + "/remoteAddr")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/remoteAddr", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
addr1 := resp.Header.Get("X-RemoteAddr")
Expect(addr1).ToNot(Equal(""))
resp, err = client.Get("https://127.0.0.1:" + port + "/remoteAddr")
resp, err = client.Get(fmt.Sprintf("https://127.0.0.1:%d/remoteAddr", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
addr2 := resp.Header.Get("X-RemoteAddr")
Expand All @@ -146,7 +146,7 @@ var _ = Describe("HTTP tests", func() {
group, ctx := errgroup.WithContext(context.Background())
for i := 0; i < 2; i++ {
group.Go(func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://localhost:"+port+"/hello", nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://localhost:%d/hello", port), nil)
Expect(err).ToNot(HaveOccurred())
resp, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -172,7 +172,7 @@ var _ = Describe("HTTP tests", func() {
close(handlerCalled)
})

req, err := http.NewRequest(http.MethodGet, "https://localhost:"+port+"/headers/request", nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://localhost:%d/headers/request", port), nil)
Expect(err).ToNot(HaveOccurred())
req.Header.Set("foo", "bar")
req.Header.Set("lorem", "ipsum")
Expand All @@ -189,15 +189,15 @@ var _ = Describe("HTTP tests", func() {
w.Header().Set("lorem", "ipsum")
})

resp, err := client.Get("https://localhost:" + port + "/headers/response")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/headers/response", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
Expect(resp.Header.Get("foo")).To(Equal("bar"))
Expect(resp.Header.Get("lorem")).To(Equal("ipsum"))
})

It("downloads a small file", func() {
resp, err := client.Get("https://localhost:" + port + "/prdata")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/prdata", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
body, err := io.ReadAll(gbytes.TimeoutReader(resp.Body, 5*time.Second))
Expand All @@ -206,7 +206,7 @@ var _ = Describe("HTTP tests", func() {
})

It("downloads a large file", func() {
resp, err := client.Get("https://localhost:" + port + "/prdatalong")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/prdatalong", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
body, err := io.ReadAll(gbytes.TimeoutReader(resp.Body, 20*time.Second))
Expand All @@ -218,7 +218,7 @@ var _ = Describe("HTTP tests", func() {
const num = 150

for i := 0; i < num; i++ {
resp, err := client.Get("https://localhost:" + port + "/hello")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/hello", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
body, err := io.ReadAll(gbytes.TimeoutReader(resp.Body, 3*time.Second))
Expand All @@ -231,7 +231,7 @@ var _ = Describe("HTTP tests", func() {
const num = 150

for i := 0; i < num; i++ {
resp, err := client.Get("https://localhost:" + port + "/prdata")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/prdata", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
Expect(resp.Body.Close()).To(Succeed())
Expand All @@ -240,7 +240,7 @@ var _ = Describe("HTTP tests", func() {

It("posts a small message", func() {
resp, err := client.Post(
"https://localhost:"+port+"/echo",
fmt.Sprintf("https://localhost:%d/echo", port),
"text/plain",
bytes.NewReader([]byte("Hello, world!")),
)
Expand All @@ -253,7 +253,7 @@ var _ = Describe("HTTP tests", func() {

It("uploads a file", func() {
resp, err := client.Post(
"https://localhost:"+port+"/echo",
fmt.Sprintf("https://localhost:%d/echo", port),
"text/plain",
bytes.NewReader(PRData),
)
Expand All @@ -277,7 +277,7 @@ var _ = Describe("HTTP tests", func() {
})

client.Transport.(*http3.RoundTripper).DisableCompression = false
resp, err := client.Get("https://localhost:" + port + "/gzipped/hello")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/gzipped/hello", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))
Expect(resp.Uncompressed).To(BeTrue())
Expand All @@ -303,7 +303,7 @@ var _ = Describe("HTTP tests", func() {
}
})

req, err := http.NewRequest(http.MethodGet, "https://localhost:"+port+"/cancel", nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://localhost:%d/cancel", port), nil)
Expect(err).ToNot(HaveOccurred())
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
Expand Down Expand Up @@ -336,7 +336,7 @@ var _ = Describe("HTTP tests", func() {
})

r, w := io.Pipe()
req, err := http.NewRequest("PUT", "https://localhost:"+port+"/echoline", r)
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("https://localhost:%d/echoline", port), r)
Expect(err).ToNot(HaveOccurred())
rsp, err := client.Do(req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -373,7 +373,7 @@ var _ = Describe("HTTP tests", func() {
}()
})

req, err := http.NewRequest(http.MethodGet, "https://localhost:"+port+"/httpstreamer", nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://localhost:%d/httpstreamer", port), nil)
Expect(err).ToNot(HaveOccurred())
rsp, err := client.Transport.(*http3.RoundTripper).RoundTripOpt(req, http3.RoundTripOpt{DontCloseRequestStream: true})
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -431,7 +431,11 @@ var _ = Describe("HTTP tests", func() {
})

expectedEnd := time.Now().Add(deadlineDelay)
resp, err := client.Post("https://localhost:"+port+"/read-deadline", "text/plain", neverEnding('a'))
resp, err := client.Post(
fmt.Sprintf("https://localhost:%d/read-deadline", port),
"text/plain",
neverEnding('a'),
)
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))

Expand All @@ -453,7 +457,7 @@ var _ = Describe("HTTP tests", func() {

expectedEnd := time.Now().Add(deadlineDelay)

resp, err := client.Get("https://localhost:" + port + "/write-deadline")
resp, err := client.Get(fmt.Sprintf("https://localhost:%d/write-deadline", port))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(200))

Expand Down

0 comments on commit 4f69656

Please sign in to comment.