func TestExistingConnState(t *testing.T) { t.Parallel() hello := []byte("hello") fin := make(chan struct{}) okHandler := func(w http.ResponseWriter, r *http.Request) { defer close(fin) w.Write(hello) } var called int32 listener, err := net.Listen("tcp", "127.0.0.1:0") ensure.Nil(t, err) server := &http.Server{ Handler: http.HandlerFunc(okHandler), ConnState: func(c net.Conn, s http.ConnState) { atomic.AddInt32(&called, 1) }, } transport := &http.Transport{} client := &http.Client{Transport: transport} down := &httpdown.HTTP{} s := down.Serve(server, listener) res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String())) ensure.Nil(t, err) actualBody, err := ioutil.ReadAll(res.Body) ensure.Nil(t, err) ensure.DeepEqual(t, actualBody, hello) ensure.Nil(t, res.Body.Close()) ensure.Nil(t, s.Stop()) <-fin ensure.True(t, atomic.LoadInt32(&called) > 0) }
func TestNewError(t *testing.T) { givenErr := errors.New("") mux, err := ctxmux.New( func(*ctxmux.Mux) error { return givenErr }, ) ensure.True(t, mux == nil) ensure.DeepEqual(t, err, givenErr) }
func TestHandlerNotFound(t *testing.T) { var called bool mux, err := ctxmux.New( ctxmux.MuxNotFoundHandler( func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { called = true return nil }), ) ensure.Nil(t, err) hw := httptest.NewRecorder() hr := &http.Request{ Method: "GET", URL: &url.URL{ Path: "/", }, } mux.ServeHTTP(hw, hr) ensure.True(t, called) }
func TestTrue(t *testing.T) { var c capture ensure.True(&c, false) c.Equal(t, `ensure_test.go:171: expected true but got false`) }
func TestMultiStackNotFound(t *testing.T) { ensure.True(t, MultiStack(errors.New("")) == nil) }
func TestStackFrameNotFound(t *testing.T) { ensure.True(t, StackFrame(errors.New("")) == nil) }
func TestDecodeInvalidJSON(t *testing.T) { encoded := base64.URLEncoding.EncodeToString([]byte("x")) value, err := decode(encoded) ensure.True(t, value == nil) ensure.Err(t, err, regexp.MustCompile(`static: invalid URL "eA=="`)) }
func TestDecodeInvalidBase64(t *testing.T) { value, err := decode("#") ensure.True(t, value == nil) ensure.Err(t, err, regexp.MustCompile(`static: invalid URL "#"`)) }
func TestDecodeNotTwoParts(t *testing.T) { encoded := base64.URLEncoding.EncodeToString([]byte("[[]]")) value, err := decode(encoded) ensure.True(t, value == nil) ensure.Err(t, err, regexp.MustCompile(`static: invalid URL "W1tdXQ=="`)) }