From 40a26c92812a0b49a6a8335c9cb70a78fd380967 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 7 Jan 2016 11:57:49 -0800 Subject: [PATCH] net/http: add some tests around sending & receiving star requests I thought there was still work to do in http2 for this, but I guess not: the work for parsing them is in net/url (used by http2) and the handling of OPTIONS * is already in net/http serverHandler, also used by http2. But keep the tests. Change-Id: I566dd0a03cf13c9ea8e735c6bd32d2c521ed503b Reviewed-on: https://go-review.googlesource.com/18368 Reviewed-by: Blake Mizerany Run-TryBot: Brad Fitzpatrick --- src/net/http/clientserver_test.go | 69 +++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/net/http/clientserver_test.go b/src/net/http/clientserver_test.go index 4f7dac2067..1a9ce5ab40 100644 --- a/src/net/http/clientserver_test.go +++ b/src/net/http/clientserver_test.go @@ -792,3 +792,72 @@ func testTransportUserAgent(t *testing.T, h2 bool) { } } } + +func TestStarRequestFoo_h1(t *testing.T) { testStarRequest(t, "FOO", h1Mode) } +func TestStarRequestFoo_h2(t *testing.T) { testStarRequest(t, "FOO", h2Mode) } +func TestStarRequestOptions_h1(t *testing.T) { testStarRequest(t, "OPTIONS", h1Mode) } +func TestStarRequestOptions_h2(t *testing.T) { testStarRequest(t, "OPTIONS", h2Mode) } +func testStarRequest(t *testing.T, method string, h2 bool) { + defer afterTest(t) + gotc := make(chan *Request, 1) + cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { + w.Header().Set("foo", "bar") + gotc <- r + w.(Flusher).Flush() + })) + defer cst.close() + + u, err := url.Parse(cst.ts.URL) + if err != nil { + t.Fatal(err) + } + u.Path = "*" + + req := &Request{ + Method: method, + Header: Header{}, + URL: u, + } + + res, err := cst.c.Do(req) + if err != nil { + t.Fatalf("RoundTrip = %v", err) + } + res.Body.Close() + + wantFoo := "bar" + wantLen := int64(-1) + if method == "OPTIONS" { + wantFoo = "" + wantLen = 0 + } + if res.StatusCode != 200 { + t.Errorf("status code = %v; want %d", res.Status, 200) + } + if res.ContentLength != wantLen { + t.Errorf("content length = %v; want %d", res.ContentLength, wantLen) + } + if got := res.Header.Get("foo"); got != wantFoo { + t.Errorf("response \"foo\" header = %q; want %q", got, wantFoo) + } + select { + case req = <-gotc: + default: + req = nil + } + if req == nil { + if method != "OPTIONS" { + t.Fatalf("handler never got request") + } + return + } + if req.Method != method { + t.Errorf("method = %q; want %q", req.Method, method) + } + if req.URL.Path != "*" { + t.Errorf("URL.Path = %q; want *", req.URL.Path) + } + if req.RequestURI != "*" { + t.Errorf("RequestURI = %q; want *", req.RequestURI) + } +} -- 2.48.1