// Verifies we don't path.Clean() on the wrong parts in redirects. func TestRedirectMunging(t *testing.T) { req, _ := NewRequest("GET", "http://example.com/", nil) resp := httptest.NewRecorder() Redirect(resp, req, "/foo?next=http://bar.com/", 302) if g, e := resp.Header().Get("Location"), "/foo?next=http://bar.com/"; g != e { t.Errorf("Location header was %q; want %q", g, e) } resp = httptest.NewRecorder() Redirect(resp, req, "http://localhost:8080/_ah/login?continue=http://localhost:8080/", 302) if g, e := resp.Header().Get("Location"), "http://localhost:8080/_ah/login?continue=http://localhost:8080/"; g != e { t.Errorf("Location header was %q; want %q", g, e) } }
func runCgiTest(t *testing.T, h *Handler, httpreq string, expectedMap map[string]string) *httptest.ResponseRecorder { rw := httptest.NewRecorder() req := newRequest(httpreq) h.ServeHTTP(rw, req) // Make a map to hold the test map that the CGI returns. m := make(map[string]string) linesRead := 0 readlines: for { line, err := rw.Body.ReadString('\n') switch { case err == os.EOF: break readlines case err != nil: t.Fatalf("unexpected error reading from CGI: %v", err) } linesRead++ trimmedLine := strings.TrimRight(line, "\r\n") split := strings.SplitN(trimmedLine, "=", 2) if len(split) != 2 { t.Fatalf("Unexpected %d parts from invalid line number %v: %q; existing map=%v", len(split), linesRead, line, m) } m[split[0]] = split[1] } for key, expected := range expectedMap { if got := m[key]; got != expected { t.Errorf("for key %q got %q; expected %q", key, got, expected) } } return rw }
func TestOptions(t *testing.T) { for _, test := range optionsTests { desc := test.Path r, err := http.NewRequest("OPTIONS", test.Path, nil) if err != nil { t.Errorf("%s - newrequest: %s", desc, err) continue } w := httptest.NewRecorder() DefaultServeMux.ServeHTTP(w, r) if got, want := strings.Join(w.HeaderMap["Allow"], ", "), test.Allow; got != want { t.Errorf("%s - allow = %q, want %q", desc, got, want) } } }
func TestHandle(t *testing.T) { for _, test := range handleTests { desc := test.Method + " " + test.Path r, err := http.NewRequest(test.Method, test.Path, bytes.NewBufferString(test.Body)) if err != nil { t.Errorf("%s - newrequest: %s", desc, err) continue } w := httptest.NewRecorder() r.RemoteAddr = "unittest" DefaultServeMux.ServeHTTP(w, r) if got, want := w.Code, test.ErrCode; got != want { t.Errorf("%s - code = %v, want %v", desc, got, want) } if bytes.Index(w.Body.Bytes(), []byte(test.Contains)) < 0 { t.Errorf("%s - body does not contain %q:", desc, test.Contains) t.Errorf("%s", w.Body.String()) } } }
func TestEnumerateInput(t *testing.T) { enumerator := &emptyEnumerator{} emptyOutput := "{\n \"blobs\": [\n\n ],\n \"canLongPoll\": true\n}\n" tests := []enumerateInputTest{ {"no 'after' with 'maxwaitsec'", "http://example.com/camli/enumerate-blobs?after=foo&maxwaitsec=1", 400, errMsgMaxWaitSecWithAfter}, {"'maxwaitsec' of 0 is okay with 'after'", "http://example.com/camli/enumerate-blobs?after=foo&maxwaitsec=0", 200, emptyOutput}, } for _, test := range tests { wr := httptest.NewRecorder() wr.Code = 200 // default req := makeGetRequest(test.url) handleEnumerateBlobs(wr, req, enumerator) ExpectInt(t, test.expectedCode, wr.Code, "response code for "+test.name) ExpectString(t, test.expectedBody, wr.Body.String(), "output for "+test.name) } }
// Tests for http://code.google.com/p/go/issues/detail?id=900 func TestMuxRedirectLeadingSlashes(t *testing.T) { paths := []string{"//foo.txt", "///foo.txt", "/../../foo.txt"} for _, path := range paths { req, err := ReadRequest(bufio.NewReader(bytes.NewBufferString("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n"))) if err != nil { t.Errorf("%s", err) } mux := NewServeMux() resp := httptest.NewRecorder() mux.ServeHTTP(resp, req) if loc, expected := resp.Header().Get("Location"), "/foo.txt"; loc != expected { t.Errorf("Expected Location header set to %q; got %q", expected, loc) return } if code, expected := resp.Code, StatusMovedPermanently; code != expected { t.Errorf("Expected response code of StatusMovedPermanently; got %d", code) return } } }
func TestFileServerCleans(t *testing.T) { ch := make(chan string, 1) fs := FileServer(&testFileSystem{func(name string) (File, error) { ch <- name return nil, os.ENOENT }}) tests := []struct { reqPath, openArg string }{ {"/foo.txt", "/foo.txt"}, {"//foo.txt", "/foo.txt"}, {"/../foo.txt", "/foo.txt"}, } req, _ := NewRequest("GET", "http://example.com", nil) for n, test := range tests { rec := httptest.NewRecorder() req.URL.Path = test.reqPath fs.ServeHTTP(rec, req) if got := <-ch; got != test.openArg { t.Errorf("test %d: got %q, want %q", n, got, test.openArg) } } }
func TestPublishURLs(t *testing.T) { owner := blobref.MustParse("owner-123") picNode := blobref.MustParse("picpn-123") galRef := blobref.MustParse("gal-123") rootRef := blobref.MustParse("root-abc") camp0 := blobref.MustParse("picpn-98765432100") camp1 := blobref.MustParse("picpn-98765432111") camp0f := blobref.MustParse("picfile-f00f00f00a5") camp1f := blobref.MustParse("picfile-f00f00f00b6") rootName := "foo" for ti, tt := range publishURLTests { idx := test.NewFakeIndex() idx.AddSignerAttrValue(owner, "camliRoot", rootName, rootRef) sh := search.NewHandler(idx, owner) ph := &PublishHandler{ RootName: rootName, Search: sh, } idx.AddMeta(owner, "text/x-openpgp-public-key", 100) for _, br := range []*blobref.BlobRef{picNode, galRef, rootRef, camp0, camp1} { idx.AddMeta(br, "application/json; camliType=permanode", 100) } for _, br := range []*blobref.BlobRef{camp0f, camp1f} { idx.AddMeta(br, "application/json; camliType=file", 100) } idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:singlepic", picNode.String()) idx.AddClaim(owner, rootRef, "set-attribute", "camliPath:camping", galRef.String()) idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp0.String()) idx.AddClaim(owner, galRef, "add-attribute", "camliMember", camp1.String()) idx.AddClaim(owner, camp0, "set-attribute", "camliContent", camp0f.String()) idx.AddClaim(owner, camp1, "set-attribute", "camliContent", camp1f.String()) rw := httptest.NewRecorder() if !strings.HasPrefix(tt.path, "/pics/") { panic("expected /pics/ prefix on " + tt.path) } req, _ := http.NewRequest("GET", "http://foo.com"+tt.path, nil) pfxh := &httputil.PrefixHandler{ Prefix: "/pics/", Handler: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { pr := ph.NewRequest(rw, req) err := pr.findSubject() if tt.subject != "" { if err != nil { t.Errorf("test #%d, findSubject: %v", ti, err) return } if pr.subject.String() != tt.subject { t.Errorf("test #%d, got subject %q, want %q", ti, pr.subject, tt.subject) } } if pr.subres != tt.subres { t.Errorf("test #%d, got subres %q, want %q", ti, pr.subres, tt.subres) } }), } pfxh.ServeHTTP(rw, req) } }