func TestInvalidWriteHeader(t *testing.T) { r := &reporter{T: t} tt := tester.New(r, testApp) tt.Get("/invalid-write-header", nil).Expect(nil) if r.err == nil || !strings.Contains(r.err.Error(), "WriteHeader() called with invalid code") { t.Errorf("expecting invalid WriteHeader() error, got %s", r.err) } }
func TestInvalidRegexp(t *testing.T) { r := &reporter{T: t} tt := tester.New(r, testApp) tt.Get("/hello", nil).Match("\\Ga+") if r.fatal == nil || !strings.Contains(r.fatal.Error(), "error compiling regular expression") { t.Errorf("expecting invalid re error, got %s", r.fatal) } }
func TestXHeaders(t *testing.T) { a := app.New() a.Handle("/", func(ctx *app.Context) { fmt.Fprintf(ctx, "%s\n%s", ctx.RemoteAddress(), ctx.URL().String()) }) tt := tester.New(t, a) tt.Get("/", nil).AddHeader("X-Real-IP", "8.8.8.8").AddHeader("X-Scheme", "https").Expect("\nhttp://localhost/") a.SetTrustXHeaders(true) tt.Get("/", nil).AddHeader("X-Real-IP", "8.8.8.8").AddHeader("X-Scheme", "https").Expect("8.8.8.8\nhttps://localhost/") }
func TestAppendSlash(t *testing.T) { a := app.New() a.Handle("/foo/", func(ctx *app.Context) { ctx.WriteString("Hello world") }) tt := tester.New(t, a) tt.Get("/foo", nil).Expect(301).ExpectHeader("Location", "/foo/") a.SetAppendSlash(false) tt.Get("/foo", nil).Expect(404) }
func TestMultipleWriteHeader(t *testing.T) { r := &reporter{T: t} tt := tester.New(r, testApp) err := tt.Get("/multiple-write-header", nil).Expect(nil).Err() if err != r.err { t.Errorf("bad error from Err()") } if r.err == nil || !strings.Contains(r.err.Error(), "WriteHeader() called 2 times") { t.Errorf("expecting multiple WriteHeader() error, got %s", r.err) } }
func TestExpectErrors(t *testing.T) { r := &reporter{T: t} tt := tester.New(r, testApp) tt.Get("/hello", nil).Expect(400) if r.err == nil { t.Error("expecting an error") } tt.Get("/hello", nil).Contains("nothing") if r.err == nil { t.Error("expecting an error") } tt.Get("/hello", nil).Expect("nothing") if r.err == nil { t.Error("expecting an error") } tt.Get("/hello", nil).ExpectHeader("X-Hello", 13) if r.err == nil { t.Error("expecting an error") } tt.Get("/hello", nil).ExpectHeader("X-Number", 37) if r.err == nil { t.Error("expecting an error") } tt.Get("/hello", nil).Expect(nil) if r.err == nil { t.Error("expecting an error") } something := []byte{1, 2, 3, 4, 5, 6} tt.Post("/echo", nil).Expect(something) if r.err == nil { t.Error("expecting an error") } tt.Post("/echo", nil).Expect(bytes.NewReader(something)) if r.err == nil { t.Error("expecting an error") } tt.Post("/echo", something).Expect(nil) if r.err == nil { t.Error("expecting an error") } tt.Post("/echo", something).Expect(float64(0)) if r.err == nil { t.Error("expecting an error") } tt.Post("/echo", float64(0)).Expect(float64(0)) if r.fatal == nil { t.Error("expecting a fatal error") } }
func TestParameters(t *testing.T) { a := app.New() a.Handle("/parse-string-param/(?P<string>\\w+)?$", func(ctx *app.Context) { str := "default" ctx.ParseParamValue("string", &str) ctx.WriteString(str) }) a.Handle("/parse-int-param/(?P<number>\\d+)?$", func(ctx *app.Context) { val := -1 ctx.ParseParamValue("number", &val) ctx.WriteString(strconv.Itoa(val)) }) a.Handle("/parse-int-form-value/$", func(ctx *app.Context) { val := -1 ctx.ParseFormValue("v", &val) ctx.WriteString(strconv.Itoa(val)) }) a.Handle("/must-parse-int-form-value/$", func(ctx *app.Context) { val := -1 ctx.MustParseFormValue("v", &val) ctx.WriteString(strconv.Itoa(val)) }) a.Handle("/parse-index-value/(\\d+)?$", func(ctx *app.Context) { val := -1 ctx.ParseIndexValue(0, &val) ctx.WriteString(strconv.Itoa(val)) }) tester := tester.New(t, a) tester.Get("/parse-string-param/", nil).Expect(200).Expect("default") tester.Get("/parse-string-param/foo", nil).Expect(200).Expect("foo") tester.Get("/parse-int-param/", nil).Expect(200).Expect("-1") // -10 does not match the handler tester.Get("/parse-int-param/-10", nil).Expect(404) tester.Get("/parse-int-param/42", nil).Expect(200).Expect("42") tester.Get("/parse-int-form-value/", nil).Expect(200).Expect("-1") tester.Get("/parse-int-form-value/", map[string]interface{}{"v": 9000}).Expect(200).Expect("9000") tester.Get("/parse-int-form-value/", map[string]interface{}{"v": "not-a-number"}).Expect(200).Expect("-1") tester.Get("/must-parse-int-form-value/", nil).Expect(400) tester.Get("/must-parse-int-form-value/", map[string]interface{}{"v": 9000}).Expect(200).Expect("9000") tester.Get("/must-parse-int-form-value/", map[string]interface{}{"v": "not-a-number"}).Expect(400) tester.Get("/parse-index-value/", nil).Expect(200).Expect("-1") tester.Get("/parse-index-value/42", nil).Expect(200).Expect("42") }
func TestGoWait(t *testing.T) { a := app.New() a.Handle("/(no)?wait", func(ctx *app.Context) { value := 42 ctx.Go(func(bg *app.Context) { time.Sleep(time.Second) value++ panic("handled") }) if ctx.IndexValue(0) != "no" { ctx.Wait() } fmt.Fprintf(ctx, "%d", value) }) tt := tester.New(t, a) tt.Get("/wait", nil).Expect("43") tt.Get("/nowait", nil).Expect("42") }
func TestExpect(t *testing.T) { tt := tester.New(t, testApp) tt.Get("/hello", nil).Expect(200).Contains("hello").Expect("hello world").Match("\\w+ \\w+"). ExpectHeader("X-Hello", "World").ExpectHeader("X-Number", 42).ContainsHeader("X-Hello", "Wo"). MatchHeader("X-Hello", "W.*d") tt.Post("/does-not-exist", nil).Expect(404) echoData := []byte{1, 2, 3, 4, 5, 6} tt.Post("/echo", echoData).Expect(echoData) tt.Post("/echo", echoData).Expect(bytes.NewReader(echoData)) tt.Post("/echo", string(echoData)).Expect(echoData) tt.Post("/echo", echoData).Expect(string(echoData)) tt.Post("/echo", bytes.NewReader(echoData)).Expect(echoData) tt.Post("/echo", nil).Expect(200).Expect("") tt.Post("/echo", nil).Expect(200).Expect(nil) form := map[string]interface{}{"foo": 1, "bar": "baz"} formExpect := "bar=baz\nfoo=1\n" tt.Form("/echo-form", form).Expect(formExpect) tt.Get("/echo-form", form).Expect(formExpect) }