示例#1
0
func TestRequestData(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123", strings.NewReader("{\"something\":true}"))

	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)

	bod, _ := c.RequestBody()
	assert.Equal(t, "{\"something\":true}", string(bod))
	dat, datErr := c.RequestData()

	if assert.NoError(t, datErr) {
		assert.Equal(t, true, dat.(map[string]interface{})["something"])
	}

	responseWriter = new(http_test.TestResponseWriter)
	testRequest, _ = http.NewRequest("GET", "http://goweb.org/people/123?body={\"something\":true}", nil)

	codecService = codecsservices.NewWebCodecService()

	c = NewWebContext(responseWriter, testRequest, codecService)

	bod, _ = c.RequestBody()
	assert.Equal(t, "{\"something\":true}", string(bod))
	dat, datErr = c.RequestData()

	if assert.NoError(t, datErr) {
		assert.Equal(t, true, dat.(map[string]interface{})["something"])
	}

}
示例#2
0
func TestErrorHandlerGetsUsedOnError(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil)

	codecService := codecsservices.NewWebCodecService()
	handler := NewHttpHandler(codecService)

	errorHandler := new(handlers_test.TestHandler)
	handler.SetErrorHandler(errorHandler)

	errorHandler.On("Handle", mock.Anything).Return(false, nil)

	// make a handler throw an error
	var theError error = errors.New("Test error")
	handler.Map(func(c context.Context) error {
		return theError
	})

	handler.ServeHTTP(responseWriter, testRequest)

	if mock.AssertExpectationsForObjects(t, errorHandler.Mock) {

		// get the first context
		ctx := errorHandler.Calls[0].Arguments[0].(context.Context)

		// make sure the error data field was set
		assert.Equal(t, theError.Error(), ctx.Data().Get("error").Data().(HandlerError).Error(), "the error should be set in the data with the 'error' key")

		assert.Equal(t, responseWriter, ctx.HttpResponseWriter())
		assert.Equal(t, testRequest, ctx.HttpRequest())

	}

}
示例#3
0
func TestMapStaticFile(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	h.MapStaticFile("/static-file", "/location/of/static-file")

	assert.Equal(t, 1, len(h.HandlersPipe()))

	staticHandler := h.HandlersPipe()[0].(*PathMatchHandler)

	if assert.Equal(t, 1, len(staticHandler.HttpMethods)) {
		assert.Equal(t, goweb_http.MethodGet, staticHandler.HttpMethods[0])
	}

	var ctx context.Context
	var willHandle bool

	ctx = context_test.MakeTestContextWithPath("/static-file")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("static-file")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("static-file/")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("static-file/something-else")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.False(t, willHandle, "Static handler NOT should handle")

}
示例#4
0
func TestPrependPreHandler(t *testing.T) {

	handler1 := new(handlers_test.TestHandler)
	handler2 := new(handlers_test.TestHandler)
	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	handler1.TestData().Set("id", 1)
	handler2.TestData().Set("id", 2)

	handler1.On("WillHandle", mock.Anything).Return(true, nil)
	handler1.On("Handle", mock.Anything).Return(false, nil)
	handler2.On("WillHandle", mock.Anything).Return(true, nil)
	handler2.On("Handle", mock.Anything).Return(false, nil)

	h.PrependPreHandler(handler1)
	h.PrependPreHandler(handler2)
	h.Handlers.Handle(nil)
	assert.Equal(t, 2, len(h.PreHandlersPipe()))

	assert.Equal(t, 2, h.PreHandlersPipe()[0].(*handlers_test.TestHandler).TestData().Get("id").Data())
	assert.Equal(t, 1, h.PreHandlersPipe()[1].(*handlers_test.TestHandler).TestData().Get("id").Data())

	mock.AssertExpectationsForObjects(t, handler1.Mock)

}
示例#5
0
func TestBeforeHandler(t *testing.T) {

	cont := new(controllers_test.TestHandlerWithBeforeAndAfters)

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	h.MapController(cont)

	log.Printf("%s", h)

	if assert.Equal(t, 2, len(h.PreHandlersPipe()), "2 pre handler's expected") {
		assertPathMatchHandler(t, h.PreHandlersPipe()[0].(*PathMatchHandler), "/test", "POST", "before POST /test")
		assertPathMatchHandler(t, h.PreHandlersPipe()[1].(*PathMatchHandler), "/test/123", "PUT", "before PUT /test/123")
		assertPathMatchHandler(t, h.PreHandlersPipe()[0].(*PathMatchHandler), "/test", "OPTIONS", "before OPTIONS /test")
		assertPathMatchHandler(t, h.PreHandlersPipe()[1].(*PathMatchHandler), "/test/123", "OPTIONS", "before OPTIONS /test/123")
	}

	if assert.Equal(t, 2, len(h.PostHandlersPipe()), "2 post handler's expected") {
		assertPathMatchHandler(t, h.PostHandlersPipe()[0].(*PathMatchHandler), "/test", "POST", "after POST /test")
		assertPathMatchHandler(t, h.PostHandlersPipe()[1].(*PathMatchHandler), "/test/123", "PUT", "after PUT /test/123")
		assertPathMatchHandler(t, h.PostHandlersPipe()[0].(*PathMatchHandler), "/test", "OPTIONS", "after OPTIONS /test")
		assertPathMatchHandler(t, h.PostHandlersPipe()[1].(*PathMatchHandler), "/test/123", "OPTIONS", "after OPTIONS /test/123")
	}

}
示例#6
0
func TestRespondEnvelopOptions(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContextWithPath("/?envelop=false")
	data := map[string]interface{}{"name": "Mat"}

	// When AlwaysEvenlopResponse = true but ?envelop=false
	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false
	ctx = context_test.MakeTestContext()
	API.AlwaysEnvelopResponse = false

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"name\":\"Mat\"}")

	// When AlwaysEvenlopResponse = false but ?envelop=true
	ctx = context_test.MakeTestContextWithPath("/?envelop=true")

	API.Respond(ctx, 200, data, nil)
	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"name\":\"Mat\"},\"s\":200}")

}
示例#7
0
func getJsonCodec() services.CodecService {
	codecService := services.NewWebCodecService()
	myCodecService := new(services.WebCodecService)
	codec, _ := codecService.GetCodec("application/json")
	myCodecService.AddCodec(codec)
	return myCodecService
}
示例#8
0
func TestFileExtension(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	codecService := codecsservices.NewWebCodecService()

	testRequest, _ := http.NewRequest("get", "http://goweb.org/people/123.json", nil)
	c := NewWebContext(responseWriter, testRequest, codecService)
	assert.Equal(t, ".json", c.FileExtension())

	testRequest, _ = http.NewRequest("get", "http://goweb.org/people/123.bson", nil)
	c = NewWebContext(responseWriter, testRequest, codecService)
	assert.Equal(t, ".bson", c.FileExtension())

	testRequest, _ = http.NewRequest("get", "http://goweb.org/people/123.xml", nil)
	c = NewWebContext(responseWriter, testRequest, codecService)
	assert.Equal(t, ".xml", c.FileExtension())

	testRequest, _ = http.NewRequest("get", "http://goweb.org/people.with.dots/123.xml", nil)
	c = NewWebContext(responseWriter, testRequest, codecService)
	assert.Equal(t, ".xml", c.FileExtension())

	testRequest, _ = http.NewRequest("get", "http://goweb.org/people.with.dots/123.xml?a=b", nil)
	c = NewWebContext(responseWriter, testRequest, codecService)
	assert.Equal(t, ".xml", c.FileExtension())

}
示例#9
0
// GetCodecService gets the codec service that will be used by this object.
func (a *GowebAPIResponder) GetCodecService() codecsservices.CodecService {

	if a.codecService == nil {
		a.codecService = codecsservices.NewWebCodecService()
	}

	return a.codecService
}
示例#10
0
func TestNewHttpHandler(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	assert.Equal(t, 3, len(h.Handlers))
	assert.Equal(t, codecService, h.CodecService())

}
示例#11
0
func TestOptionsListForSingleResource(t *testing.T) {
	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)
	c := new(test.TestController)
	assert.Equal(t, "GET,DELETE,PATCH,PUT,HEAD,OPTIONS", strings.Join(optionsListForSingleResource(h, c), ","))

	c2 := new(test.TestSemiRestfulController)
	assert.Equal(t, "GET,OPTIONS", strings.Join(optionsListForSingleResource(h, c2), ","))

}
示例#12
0
func TestMapController_WithExplicitHTTPMethods(t *testing.T) {

	rest := new(controllers_test.TestController)

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	h.HttpMethodForCreate = "CREATE"
	h.HttpMethodForReadOne = "READ_ONE"
	h.HttpMethodForReadMany = "READ_MANY"
	h.HttpMethodForDeleteOne = "DELETE_ONE"
	h.HttpMethodForDeleteMany = "DELETE_MANY"
	h.HttpMethodForUpdateOne = "UPDATE_ONE"
	h.HttpMethodForUpdateMany = "UPDATE_MANY"
	h.HttpMethodForReplace = "REPLACE"
	h.HttpMethodForHead = "HEAD_CUSTOM"
	h.HttpMethodForOptions = "OPTIONS_CUSTOM"

	h.MapController(rest)

	assert.Equal(t, 10, len(h.HandlersPipe()))

	// create
	assertPathMatchHandler(t, h.HandlersPipe()[0].(*PathMatchHandler), "/test", "CREATE", "create")

	// read one
	assertPathMatchHandler(t, h.HandlersPipe()[1].(*PathMatchHandler), "/test/123", "READ_ONE", "read one")

	// read many
	assertPathMatchHandler(t, h.HandlersPipe()[2].(*PathMatchHandler), "/test", "READ_MANY", "read many")

	// delete one
	assertPathMatchHandler(t, h.HandlersPipe()[3].(*PathMatchHandler), "/test/123", "DELETE_ONE", "delete one")

	// delete many
	assertPathMatchHandler(t, h.HandlersPipe()[4].(*PathMatchHandler), "/test", "DELETE_MANY", "delete many")

	// update one
	assertPathMatchHandler(t, h.HandlersPipe()[5].(*PathMatchHandler), "/test/123", "UPDATE_ONE", "update one")

	// update many
	assertPathMatchHandler(t, h.HandlersPipe()[6].(*PathMatchHandler), "/test", "UPDATE_MANY", "update many")

	// replace one
	assertPathMatchHandler(t, h.HandlersPipe()[7].(*PathMatchHandler), "/test/123", "REPLACE", "replace")

	// head
	assertPathMatchHandler(t, h.HandlersPipe()[8].(*PathMatchHandler), "/test/123", "HEAD_CUSTOM", "head")
	assertPathMatchHandler(t, h.HandlersPipe()[8].(*PathMatchHandler), "/test", "HEAD_CUSTOM", "head")

	// options
	assertPathMatchHandler(t, h.HandlersPipe()[9].(*PathMatchHandler), "/test/123", "OPTIONS_CUSTOM", "options")
	assertPathMatchHandler(t, h.HandlersPipe()[9].(*PathMatchHandler), "/test", "OPTIONS_CUSTOM", "options")

}
示例#13
0
func TestHandlerForOptions_PlainHandler(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	httpHandler := NewHttpHandler(codecService)
	handler1 := new(handlers_test.TestHandler)

	itself, _ := httpHandler.handlerForOptions(handler1)

	assert.Equal(t, handler1, itself, "handlerForOptions with existing handler should just return the handler")

}
示例#14
0
func TestAPI_RespondWithError(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	errObject := "error message"

	API.RespondWithError(ctx, 500, errObject)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"e\":[\"error message\"],\"s\":500}")

}
示例#15
0
func TestRespond(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := map[string]interface{}{"name": "Mat"}

	API.Respond(ctx, 200, data, nil)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"name\":\"Mat\"},\"s\":200}")

}
示例#16
0
func TestRespondWithPublicDataFacade(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := new(dataObject)

	API.Respond(ctx, 200, data, nil)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"d\":{\"used-public-data\":true},\"s\":200}")

}
示例#17
0
func TestMapStatic(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	h.MapStatic("/static", "/location/of/static")

	assert.Equal(t, 1, len(h.HandlersPipe()))

	staticHandler := h.HandlersPipe()[0].(*PathMatchHandler)

	if assert.Equal(t, 1, len(staticHandler.HttpMethods)) {
		assert.Equal(t, goweb_http.MethodGet, staticHandler.HttpMethods[0])
	}

	var ctx context.Context
	var willHandle bool

	ctx = context_test.MakeTestContextWithPath("/static/some/deep/file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/../static/some/deep/file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/some/../file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/../file.dat")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.False(t, willHandle, "Static handler should not handle")

	ctx = context_test.MakeTestContextWithPath("/static")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/")
	willHandle, _ = staticHandler.WillHandle(ctx)
	assert.True(t, willHandle, "Static handler should handle")

	ctx = context_test.MakeTestContextWithPath("/static/doc.go")
	willHandle, _ = staticHandler.WillHandle(ctx)
	_, staticHandleErr := staticHandler.Handle(ctx)

	if assert.NoError(t, staticHandleErr) {

	}

}
示例#18
0
func TestNewGowebAPIResponder(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	api := NewGowebAPIResponder(codecService, http)

	assert.Equal(t, http, api.httpResponder)
	assert.Equal(t, codecService, api.GetCodecService())

	assert.Equal(t, api.StandardFieldStatusKey, "s")
	assert.Equal(t, api.StandardFieldDataKey, "d")
	assert.Equal(t, api.StandardFieldErrorsKey, "e")

}
示例#19
0
func TestQueryValue(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123?name=Mat&name=Laurie&age=30&something=true", strings.NewReader("[{\"something\":true},{\"something\":false}]"))

	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)

	assert.Equal(t, "Mat", c.QueryValue("name"), "QueryValue should get first value")
	assert.Equal(t, "30", c.QueryValue("age"))
	assert.Equal(t, "", c.QueryValue("no-such-value"))

}
示例#20
0
func TestPathParams(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123", nil)

	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)
	c.Data().Set(context.DataKeyPathParameters, objx.Map{"animal": "monkey"})

	assert.Equal(t, "monkey", c.PathParams().Get("animal").Data())
	assert.Equal(t, "monkey", c.PathValue("animal"))
	assert.Equal(t, "", c.PathValue("doesn't exist"))

}
示例#21
0
func TestAppendPostHandler(t *testing.T) {

	handler1 := new(handlers_test.TestHandler)
	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	handler1.On("WillHandle", mock.Anything).Return(true, nil)
	handler1.On("Handle", mock.Anything).Return(false, nil)

	h.AppendPostHandler(handler1)
	h.Handlers.Handle(nil)
	assert.Equal(t, 1, len(h.PostHandlersPipe()))

	mock.AssertExpectationsForObjects(t, handler1.Mock)

}
示例#22
0
func TestRespondWithCustomFieldnames(t *testing.T) {

	http := new(GowebHTTPResponder)
	codecService := codecsservices.NewWebCodecService()
	API := NewGowebAPIResponder(codecService, http)
	ctx := context_test.MakeTestContext()
	data := map[string]interface{}{"name": "Mat"}

	API.StandardFieldDataKey = "data"
	API.StandardFieldStatusKey = "status"

	API.Respond(ctx, 200, data, nil)

	assert.Equal(t, context_test.TestResponseWriter.Output, "{\"data\":{\"name\":\"Mat\"},\"status\":200}")

}
示例#23
0
func TestMapStaticFile_WithMatcherFuncs(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)

	matcherFunc := MatcherFunc(func(c context.Context) (MatcherFuncDecision, error) {
		return Match, nil
	})

	h.MapStaticFile("/static-file", "/location/of/static-file", matcherFunc)

	assert.Equal(t, 1, len(h.HandlersPipe()))
	staticHandler := h.HandlersPipe()[0].(*PathMatchHandler)
	assert.Equal(t, 1, len(staticHandler.MatcherFuncs))
	assert.Equal(t, matcherFunc, staticHandler.MatcherFuncs[0], "Matcher func (first)")
}
示例#24
0
func TestFormValue(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("POST", "http://goweb.org/people/123?query=yes", strings.NewReader("name=Mat&name=Laurie&age=30&something=true"))
	testRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)

	assert.Equal(t, "Mat", c.FormValue("name"), "QueryValue should get first value")
	assert.Equal(t, "30", c.FormValue("age"))
	assert.Equal(t, "", c.FormValue("no-such-value"))
	assert.Equal(t, "yes", c.FormValue("query"))

}
示例#25
0
func TestGetAndSetErrorHandler(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	handler := NewHttpHandler(codecService)

	errorHandler := new(handlers_test.TestHandler)

	// default one should be made
	assert.NotNil(t, handler.ErrorHandler())

	//... but if we set one explicitally
	handler.SetErrorHandler(errorHandler)

	//... it should be set!
	assert.Equal(t, errorHandler, handler.ErrorHandler())

}
示例#26
0
func TestMapController_DefaultOptions(t *testing.T) {

	semi := new(controllers_test.TestSemiRestfulController)

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)
	h.MapController(semi)

	assert.Equal(t, 5, len(h.HandlersPipe()))

	// get the last two
	handler1 := h.HandlersPipe()[len(h.HandlersPipe())-2]

	handler2 := h.HandlersPipe()[len(h.HandlersPipe())-1]
	assertPathMatchHandler(t, handler1.(*PathMatchHandler), "/test-semi-restful", "OPTIONS", "options")
	assertPathMatchHandler(t, handler2.(*PathMatchHandler), "/test-semi-restful/{id}", "OPTIONS", "options")

}
示例#27
0
func TestMapController_WithSpecificPath(t *testing.T) {

	rest := new(controllers_test.TestController)

	codecService := codecsservices.NewWebCodecService()
	h := NewHttpHandler(codecService)
	h.MapController("something", rest)

	assert.Equal(t, 10, len(h.HandlersPipe()))

	// create
	assertPathMatchHandler(t, h.HandlersPipe()[0].(*PathMatchHandler), "/something", "POST", "create")

	// read one
	assertPathMatchHandler(t, h.HandlersPipe()[1].(*PathMatchHandler), "/something/123", "GET", "read one")

	// read many
	assertPathMatchHandler(t, h.HandlersPipe()[2].(*PathMatchHandler), "/something", "GET", "read many")

	// delete one
	assertPathMatchHandler(t, h.HandlersPipe()[3].(*PathMatchHandler), "/something/123", "DELETE", "delete one")

	// delete many
	assertPathMatchHandler(t, h.HandlersPipe()[4].(*PathMatchHandler), "/something", "DELETE", "delete many")

	// update one
	assertPathMatchHandler(t, h.HandlersPipe()[5].(*PathMatchHandler), "/something/123", "PATCH", "update one")

	// update many
	assertPathMatchHandler(t, h.HandlersPipe()[6].(*PathMatchHandler), "/something", "PATCH", "update many")

	// replace one
	assertPathMatchHandler(t, h.HandlersPipe()[7].(*PathMatchHandler), "/something/123", "PUT", "replace")

	// head
	assertPathMatchHandler(t, h.HandlersPipe()[8].(*PathMatchHandler), "/something/123", "HEAD", "head")
	assertPathMatchHandler(t, h.HandlersPipe()[8].(*PathMatchHandler), "/something", "HEAD", "head")

	// options
	assertPathMatchHandler(t, h.HandlersPipe()[9].(*PathMatchHandler), "/something/123", "OPTIONS", "options")
	assertPathMatchHandler(t, h.HandlersPipe()[9].(*PathMatchHandler), "/something", "OPTIONS", "options")

}
示例#28
0
func TestQueryValues(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123?name=Mat&name=Laurie&age=30&something=true", strings.NewReader("[{\"something\":true},{\"something\":false}]"))

	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)

	names := c.QueryValues("name")

	if assert.Equal(t, 2, len(names)) {
		assert.Equal(t, "Mat", names[0])
		assert.Equal(t, "Laurie", names[1])
	}

	assert.Nil(t, c.QueryValues("no-such-value"))

}
示例#29
0
func TestNewContext(t *testing.T) {

	responseWriter := new(http_test.TestResponseWriter)
	testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123", nil)
	codecService := codecsservices.NewWebCodecService()

	c := NewWebContext(responseWriter, testRequest, codecService)

	if assert.NotNil(t, c) {

		assert.Equal(t, "people/123", c.Path().RawPath)
		assert.Equal(t, testRequest, c.httpRequest)
		assert.Equal(t, responseWriter, c.httpResponseWriter)
		assert.Equal(t, codecService, c.codecService)
		assert.Equal(t, codecService, c.CodecService())

	}

}
示例#30
0
func TestMap(t *testing.T) {

	codecService := codecsservices.NewWebCodecService()
	handler := NewHttpHandler(codecService)

	called := false
	handler.Map("/people/{id}", func(c context.Context) error {
		called = true
		return nil
	})

	assert.Equal(t, 1, len(handler.HandlersPipe()))

	ctx := context_test.MakeTestContextWithPath("people/123")
	handler.Handlers.Handle(ctx)

	assert.True(t, called)

}