func TestRequestAlreadyDispatched(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world") })) defer ts.Close() req := NewRequest() req.UseRequest(func(ctx *context.Context, h context.Handler) { h.Next(ctx) }) req.UseRequest(func(ctx *context.Context, h context.Handler) { u, _ := url.Parse(ts.URL) ctx.Request.URL = u h.Next(ctx) }) res, err := req.Send() st.Expect(t, err, nil) st.Reject(t, res.RawRequest.URL, nil) st.Expect(t, res.StatusCode, 200) res, err = req.Send() st.Reject(t, err, nil) }
func TestConnClose(t *testing.T) { c, err := NewConn(testDial(t)) st.Expect(t, err, nil) st.Reject(t, c, nil) st.Reject(t, c.Greeting.ServerName, "") err = c.Close() st.Expect(t, err, nil) }
// Allows comparing non-comparable types to prevent panics when comparing slices // or maps. func TestDeeperEquality(t *testing.T) { type testStr string slice1 := []interface{}{"A", 1, []byte("steak sauce")} slice2 := []interface{}{"R", 2, 'd', int64(2)} map1 := map[string]string{"clever": "crafty", "modest": "prim"} map2 := map[string]string{"silk": "scarf", "wool": "sweater"} str1 := "same" str2 := testStr("same") st.Expect(t, slice1, slice2) st.Reject(t, slice1, slice1) st.Expect(t, map1, map2) st.Reject(t, map1, map1) st.Expect(t, str1, str2) st.Reject(t, str1, str1) }
func TestResponseSaveToFileError(t *testing.T) { ctx := NewContext() ctx.Error = errors.New("foo error") res, _ := buildResponse(ctx) err := res.SaveToFile("body.tmp") st.Reject(t, err, nil) }
func TestExample(t *testing.T) { st.Expect(t, "a", "a") st.Reject(t, 42, int64(42)) st.Assert(t, "t", "t") st.Refute(t, 99, int64(99)) }
func TestMiddlewareErrorInjectionAndInterception(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world") })) defer ts.Close() req := NewRequest() req.UseRequest(func(ctx *context.Context, h context.Handler) { h.Next(ctx) }) req.UseRequest(func(ctx *context.Context, h context.Handler) { u, _ := url.Parse(ts.URL) ctx.Request.URL = u h.Error(ctx, errors.New("Error")) }) req.UseError(func(ctx *context.Context, h context.Handler) { ctx.Error = nil h.Next(ctx) }) res, err := req.Send() st.Expect(t, err, nil) st.Reject(t, res.RawRequest.URL, nil) st.Expect(t, res.StatusCode, 200) }
// Prints failure output, including the correct line number. func TestFailedExpectationMessages(t *testing.T) { t.Log("Tests purposely fail to demonstrate output") st.Expect(t, 1, 2) st.Reject(t, "same", "same") var typedNil *string st.Expect(t, typedNil, nil) // in Go, a typed nil != nil }
func TestResponseCloseError(t *testing.T) { ctx := NewContext() ctx.Error = errors.New("foo error") res, _ := buildResponse(ctx) err := res.Close() st.Reject(t, err, nil) st.Expect(t, err.Error(), "foo error") }
func TestResponseBytesError(t *testing.T) { ctx := NewContext() ctx.Error = errors.New("foo error") utils.WriteBodyString(ctx.Response, "foo bar") res, err := buildResponse(ctx) body := res.Bytes() st.Reject(t, err, nil) st.Expect(t, string(body), "") }
func TestResponseReadError(t *testing.T) { ctx := NewContext() ctx.Error = errors.New("foo error") res, _ := buildResponse(ctx) num, err := res.Read([]byte{}) st.Reject(t, err, nil) st.Expect(t, err.Error(), "foo error") st.Expect(t, num, -1) }
func TestDispatcherError(t *testing.T) { req := NewRequest() req.UseRequest(func(ctx *context.Context, h context.Handler) { u, _ := url.Parse("http://127.0.0.1:9123") ctx.Request.URL = u h.Next(ctx) }) var err error req.UseError(func(ctx *context.Context, h context.Handler) { err = ctx.Error h.Next(ctx) }) ctx := NewDispatcher(req).Dispatch() st.Reject(t, err, nil) st.Reject(t, ctx.Error, nil) }
func TestReadMIME(t *testing.T) { fns, err := whoistest.ResponseFiles() st.Assert(t, err, nil) for _, fn := range fns { // fmt.Printf("%s\n", fn) res, err := ReadMIMEFile(fn) st.Reject(t, res, nil) st.Expect(t, err, nil) // fmt.Printf("%#v\n\n", res) } }
func TestDispatcherResponseError(t *testing.T) { req := NewRequest().URL("http://127.0.0.1:9123") req.UseError(func(ctx *context.Context, h context.Handler) { ctx.Response.StatusCode = 503 h.Next(ctx) }) ctx := NewDispatcher(req).Dispatch() st.Reject(t, ctx.Error, nil) st.Expect(t, ctx.Response.StatusCode, 503) }
func TestResponseReaderBufferError(t *testing.T) { ctx := NewContext() ctx.Error = errors.New("foo error") res, err := buildResponse(ctx) body := res.Bytes() st.Reject(t, err, nil) st.Expect(t, string(body), "") st.Expect(t, res.buffer.Len(), 0) res.ClearInternalBuffer() st.Expect(t, res.buffer.Len(), 0) }
func TestResponseJSONError(t *testing.T) { type jsonData struct { Foo string `json:foo` } json := &jsonData{} ctx := NewContext() ctx.Error = errors.New("foo error") res, _ := buildResponse(ctx) err := res.JSON(json) st.Reject(t, err, nil) st.Expect(t, json.Foo, "") }
func TestConnCheck(t *testing.T) { c := testLogin(t) dcr, err := c.CheckDomain("google.com") st.Expect(t, err, nil) st.Reject(t, dcr, nil) st.Expect(t, len(dcr.Checks), 1) st.Expect(t, dcr.Checks[0].Domain, "google.com") st.Expect(t, dcr.Checks[0].Available, false) dcr, err = c.CheckDomain("dmnr-test-x759824vim-i2.com") st.Expect(t, err, nil) st.Reject(t, dcr, nil) st.Expect(t, len(dcr.Checks), 1) st.Expect(t, dcr.Checks[0].Domain, "dmnr-test-x759824vim-i2.com") st.Expect(t, dcr.Checks[0].Available, true) dcr, err = c.CheckDomain("--dmnr-test--.com") st.Reject(t, err, nil) st.Expect(t, dcr, (*DomainCheckResponse)(nil)) }
func TestResponseXMLError(t *testing.T) { type xml struct { Foo string `xml:"foo"` } xmlData := &xml{} ctx := NewContext() ctx.Error = errors.New("foo error") utils.WriteBodyString(ctx.Response, `<xml><foo>bar</foo></xml>`) res, _ := buildResponse(ctx) err := res.XML(xmlData, nil) st.Reject(t, err, nil) st.Expect(t, xmlData.Foo, "") }
func TestDispatcherStoppedMiddleware(t *testing.T) { req := NewRequest().URL("http://127.0.0.1:9123") req.UseRequest(func(ctx *context.Context, h context.Handler) { ctx.Set("foo", "bar") h.Stop(ctx) }) req.UseHandler("stop", func(ctx *context.Context, h context.Handler) { h.Error(ctx, errors.New("stop")) }) ctx := NewDispatcher(req).Dispatch() st.Expect(t, ctx.Stopped, true) st.Reject(t, ctx.Error, nil) st.Expect(t, ctx.Error.Error(), "stop") st.Expect(t, ctx.GetString("foo"), "bar") }
func TestTableExample(t *testing.T) { examples := []struct{ a, t string }{ {"first", "first"}, {"second", "second"}, } // Pass example index to improve the error message for table-based tests. for i, ex := range examples { st.Expect(t, ex, ex, i) st.Reject(t, ex, &ex, i) } // Cannot pass index into Assert or Refute, they fail fast. for _, ex := range examples { st.Assert(t, ex, ex) st.Refute(t, ex, &ex) } }
func TestRequestTimeout(t *testing.T) { if runtime.Version() != "go1.6" { return } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(1000 * time.Millisecond) fmt.Fprintln(w, "Hello, world") })) defer ts.Close() req := NewRequest().URL(ts.URL) req.UseRequest(func(ctx *context.Context, h context.Handler) { ctx.Client.Timeout = 50 * time.Millisecond h.Next(ctx) }) res, err := req.Send() st.Reject(t, err, nil) st.Expect(t, strings.Contains(err.Error(), "net/http: request canceled"), true) st.Expect(t, res.StatusCode, 0) }
// Prints failure output, including the correct line number. func TestFailedExpectationMessages(t *testing.T) { st.Expect(t, 1, 2) st.Reject(t, "same", "same") var typedNil *string st.Expect(t, typedNil, nil) // in Go, a typed nil != nil }
// Prints failure output, including the correct line number. func TestFailedRefuteMessage(t *testing.T) { st.Reject(t, 42, 7*6) }
func TestRequestCookieJar(t *testing.T) { req := NewRequest() req.CookieJar() req.Middleware.Run("request", req.Context) st.Reject(t, req.Context.Client.Jar, nil) }