Exemplo n.º 1
0
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)
}
Exemplo n.º 2
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)
}
Exemplo n.º 3
0
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)
}
Exemplo n.º 4
0
func TestTrue(t *testing.T) {
	var c capture
	ensure.True(&c, false)
	c.Equal(t, `ensure_test.go:171: expected true but got false`)
}
Exemplo n.º 5
0
func TestMultiStackNotFound(t *testing.T) {
	ensure.True(t, MultiStack(errors.New("")) == nil)
}
Exemplo n.º 6
0
func TestStackFrameNotFound(t *testing.T) {
	ensure.True(t, StackFrame(errors.New("")) == nil)
}
Exemplo n.º 7
0
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=="`))
}
Exemplo n.º 8
0
func TestDecodeInvalidBase64(t *testing.T) {
	value, err := decode("#")
	ensure.True(t, value == nil)
	ensure.Err(t, err, regexp.MustCompile(`static: invalid URL "#"`))
}
Exemplo n.º 9
0
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=="`))
}