]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add Client tests for various 3xx redirect codes
authorEmmanuel Odeke <emm.odeke@gmail.com>
Mon, 26 Sep 2016 01:31:05 +0000 (18:31 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 26 Sep 2016 01:51:31 +0000 (01:51 +0000)
Updates #13994
Updates #16840

Change-Id: Ia3cad5c211e0c688a945ed6b6277c2552592774c
Reviewed-on: https://go-review.googlesource.com/29760
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/client_test.go

index dc9995b152b70f029e8b92f5d4a634fe1a8ba6bd..77498b39132318035f29964db6043b3e9964983c 100644 (file)
@@ -1338,3 +1338,71 @@ func TestShouldCopyHeaderOnRedirect(t *testing.T) {
                }
        }
 }
+
+func TestClientRedirectTypes(t *testing.T) {
+       defer afterTest(t)
+
+       tests := [...]struct {
+               broken       int // broken is bug number
+               method       string
+               serverStatus int
+               wantMethod   string // desired subsequent client method
+       }{
+               0: {method: "POST", serverStatus: 301, wantMethod: "GET"},
+               1: {method: "POST", serverStatus: 302, wantMethod: "GET"},
+               2: {method: "POST", serverStatus: 307, wantMethod: "POST", broken: 16840},
+
+               5: {method: "GET", serverStatus: 301, wantMethod: "GET"},
+               6: {method: "GET", serverStatus: 302, wantMethod: "GET"},
+               7: {method: "GET", serverStatus: 303, wantMethod: "GET"},
+               8: {method: "GET", serverStatus: 307, wantMethod: "GET"},
+               9: {method: "GET", serverStatus: 308, wantMethod: "GET"},
+
+               10: {method: "DELETE", serverStatus: 308, wantMethod: "DELETE", broken: 13994},
+       }
+
+       handlerc := make(chan HandlerFunc, 1)
+
+       ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
+               h := <-handlerc
+               h(rw, req)
+       }))
+       defer ts.Close()
+
+       for i, tt := range tests {
+               if tt.broken != 0 {
+                       t.Logf("#%d: skipping known broken test case. See Issue #%d", i, tt.broken)
+                       continue
+               }
+
+               handlerc <- func(w ResponseWriter, r *Request) {
+                       w.Header().Set("Location", ts.URL)
+                       w.WriteHeader(tt.serverStatus)
+               }
+
+               req, err := NewRequest(tt.method, ts.URL, nil)
+               if err != nil {
+                       t.Errorf("#%d: NewRequest: %v", i, err)
+                       continue
+               }
+
+               c := &Client{}
+               c.CheckRedirect = func(req *Request, via []*Request) error {
+                       if got, want := req.Method, tt.wantMethod; got != want {
+                               return fmt.Errorf("#%d: got next method %q; want %q", i, got, want)
+                       }
+                       handlerc <- func(rw ResponseWriter, req *Request) {
+                               // TODO: Check that the body is valid when we do 307 and 308 support
+                       }
+                       return nil
+               }
+
+               res, err := c.Do(req)
+               if err != nil {
+                       t.Errorf("#%d: Response: %v", i, err)
+                       continue
+               }
+
+               res.Body.Close()
+       }
+}