func TestMapCreation(t *testing.T) { o := New(nil) assert.Nil(t, o) o = New("Tyler") assert.Nil(t, o) unconvertable := &Unconvertable{name: "Tyler"} o = New(unconvertable) assert.Nil(t, o) convertable := &Convertable{name: "Tyler"} o = New(convertable) if assert.NotNil(t, convertable) { assert.Equal(t, "Tyler", o["name"], "Tyler") } o = MSI() if assert.NotNil(t, o) { assert.NotNil(t, o) } o = MSI("name", "Tyler") if assert.NotNil(t, o) { if assert.NotNil(t, o) { assert.Equal(t, o["name"], "Tyler") } } }
func TestPathMatchHandler(t *testing.T) { pathPattern, _ := paths.NewPathPattern("collection/{id}/name") var called bool = false h := NewPathMatchHandler(pathPattern, HandlerExecutionFunc(func(c context.Context) error { called = true return nil })) ctx1 := context_test.MakeTestContextWithPath("/collection/123/name") will, _ := h.WillHandle(ctx1) assert.True(t, will) h.Handle(ctx1) assert.True(t, called, "Method should be called") assert.Equal(t, "123", ctx1.Data().Get(context.DataKeyPathParameters).ObjxMap().Get("id").Data()) ctx2 := context_test.MakeTestContextWithPath("/collection") will, _ = h.WillHandle(ctx2) assert.False(t, will) assert.Nil(t, ctx2.Data().Get(context.DataKeyPathParameters).Data()) h.BreakCurrentPipeline = true shouldStop, handleErr := h.Handle(ctx2) assert.Nil(t, handleErr) assert.True(t, shouldStop) assert.True(t, called, "Handler func should get called") }
func TestPublicDataMap_ReturningNil(t *testing.T) { o := new(test.TestObjectWithFacade) o.Mock.On("PublicData", map[string]interface{}{}).Return(nil, nil) public, err := PublicDataMap(o, map[string]interface{}{}) if assert.Nil(t, err) { assert.Nil(t, public) } mock.AssertExpectationsForObjects(t, o.Mock) }
func TestMarshalWithCodec(t *testing.T) { testCodec := new(test.TestCodec) service := NewWebCodecService() // make some test stuff var bytesToReturn []byte = []byte("Hello World") var object objx.Map = objx.MSI("Name", "Mat") var option1 string = "Option One" var option2 string = "Option Two" args := map[string]interface{}{option1: option1, option2: option2} // setup expectations testCodec.On("Marshal", object, args).Return(bytesToReturn, nil) bytes, err := service.MarshalWithCodec(testCodec, object, args) if assert.Nil(t, err) { assert.Equal(t, string(bytesToReturn), string(bytes)) } // assert that our expectations were met mock.AssertExpectationsForObjects(t, testCodec.Mock) }
func TestPublicData_WithArray(t *testing.T) { o := new(test.TestObjectWithFacade) o1 := new(test.TestObjectWithFacade) o2 := new(test.TestObjectWithFacade) arr := []interface{}{o, o1, o2} o.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "1"}), nil) o1.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "2"}), nil) o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "3"}), nil) public, err := PublicData(arr, map[string]interface{}{}) if assert.Nil(t, err) { assert.Equal(t, reflect.Slice, reflect.TypeOf(public).Kind(), "Result should be array not %v", reflect.TypeOf(public)) } mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock) publicArray := public.([]interface{}) if assert.Equal(t, 3, len(publicArray)) { assert.Equal(t, publicArray[0].(objx.Map).Get("theName").Str(), "1", "o") assert.Equal(t, publicArray[1].(objx.Map).Get("theName").Str(), "2", "o1") assert.Equal(t, publicArray[2].(objx.Map).Get("theName").Str(), "3", "o2") } }
func TestMarshalWithCodec_WithFacade(t *testing.T) { // func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) { testCodec := new(test.TestCodec) service := NewWebCodecService() // make some test stuff var bytesToReturn []byte = []byte("Hello World") testObjectWithFacade := new(test.TestObjectWithFacade) object := objx.MSI("Name", "Mat") var option1 string = "Option One" var option2 string = "Option Two" args := map[string]interface{}{option1: option1, option2: option2} // setup expectations testObjectWithFacade.On("PublicData", args).Return(object, nil) testCodec.On("Marshal", object, args).Return(bytesToReturn, nil) bytes, err := service.MarshalWithCodec(testCodec, testObjectWithFacade, args) if assert.Nil(t, err) { assert.Equal(t, string(bytesToReturn), string(bytes)) } // assert that our expectations were met mock.AssertExpectationsForObjects(t, testCodec.Mock, testObjectWithFacade.Mock) }
func TestMapFromJSONWithError(t *testing.T) { var m Map assert.Panics(t, func() { m = MustFromJSON(`"name":"Mat"}`) }) assert.Nil(t, m) }
func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) { current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}} one := access(current, 0, nil, false, false) two := access(current, 1, nil, false, false) three := access(current, 2, nil, false, false) assert.Equal(t, "Tyler", one.(map[string]interface{})["first"]) assert.Equal(t, "Capitol", two.(map[string]interface{})["first"]) assert.Nil(t, three) }
func TestPostValues(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) names := c.PostValues("name") if assert.Equal(t, 2, len(names)) { assert.Equal(t, "Mat", names[0]) assert.Equal(t, "Laurie", names[1]) } assert.Nil(t, c.PostValues("no-such-value")) assert.Nil(t, c.PostValues("query")) }
func TestUnmarshal(t *testing.T) { codec := new(BsonCodec) bsonData := []byte{0x15, 0x0, 0x0, 0x0, 0x2, 0x6e, 0x61, 0x6d, 0x65, 0x0, 0x6, 0x0, 0x0, 0x0, 0x54, 0x79, 0x6c, 0x65, 0x72, 0x0, 0x0} var object map[string]interface{} err := codec.Unmarshal(bsonData, &object) if assert.Nil(t, err) { assert.Equal(t, "Tyler", object["name"]) } }
func TestAccessorsAccessGetInsideArray(t *testing.T) { current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}} assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true)) assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true)) assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true)) assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true)) assert.Panics(t, func() { access(current, "names[2]", nil, false, true) }) assert.Nil(t, access(current, "names[2]", nil, false, false)) }
func TestPublicDataMap_WithMSI(t *testing.T) { o := new(test.TestObjectWithFacade) o.Mock.On("PublicData", map[string]interface{}{}).Return(map[string]interface{}{"theName": "Mat"}, nil) public, err := PublicDataMap(o, map[string]interface{}{}) if assert.Nil(t, err) { assert.Equal(t, public.Get("theName").Str(), "Mat") } mock.AssertExpectationsForObjects(t, o.Mock) }
func TestPublicData_WithRecursion(t *testing.T) { o := new(test.TestObjectWithFacade) o1 := new(test.TestObjectWithFacade) o2 := new(test.TestObjectWithFacade) o.Mock.On("PublicData", map[string]interface{}{}).Return(o1, nil) o1.Mock.On("PublicData", map[string]interface{}{}).Return(o2, nil) o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil) public, err := PublicData(o, map[string]interface{}{}) if assert.Nil(t, err) { assert.Equal(t, public.(objx.Map).Get("theName").Str(), "Mat") } mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock) }
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")) }
func TestPublicData_WithRecursion_WithObjects(t *testing.T) { o := new(test.TestObjectWithFacade) o1 := new(test.TestObjectWithFacade) o2 := new(test.TestObjectWithFacade) args := map[string]interface{}{constants.OptionKeyClientCallback: "~d"} o.Mock.On("PublicData", args).Return(o1, nil) o1.Mock.On("PublicData", args).Return(o2, nil) o2.Mock.On("PublicData", args).Return(objx.New(map[string]interface{}{"theName": "Mat"}), nil) public, err := PublicData(o, args) if assert.Nil(t, err) { assert.Equal(t, public.(objx.Map).Get("theName").Str(), "Mat") } mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock) }
func TestUnmarshalWithCodec(t *testing.T) { // func (s *WebCodecService) UnmarshalWithCodec(codec codecs.Codec, data []byte, object interface{}) error { testCodec := new(test.TestCodec) service := NewWebCodecService() // some test objects object := struct{}{} data := []byte("Some bytes") // setup expectations testCodec.On("Unmarshal", data, object).Return(nil) // call the target method err := service.UnmarshalWithCodec(testCodec, data, object) assert.Nil(t, err) mock.AssertExpectationsForObjects(t, testCodec.Mock) }
/* Post parameters */ func TestPostParams(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) params := c.PostParams() if assert.NotNil(t, params) { assert.Equal(t, "Mat", params.Get("name").StrSlice()[0]) assert.Equal(t, "Laurie", params.Get("name").StrSlice()[1]) assert.Equal(t, "30", params.Get("age").StrSlice()[0]) assert.Equal(t, "true", params.Get("something").StrSlice()[0]) assert.Nil(t, params.Get("query").Data()) } }