func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) { var buffer bytes.Buffer var fields Fields logger := New() logger.Out = &buffer logger.Formatter = new(JSONFormatter) llog := logger.WithField("context", "eating raw fish") llog.Info("looks delicious") err := json.Unmarshal(buffer.Bytes(), &fields) assert.NoError(t, err, "should have decoded first message") assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields") assert.Equal(t, fields["msg"], "looks delicious") assert.Equal(t, fields["context"], "eating raw fish") buffer.Reset() llog.Warn("omg it is!") err = json.Unmarshal(buffer.Bytes(), &fields) assert.NoError(t, err, "should have decoded second message") assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields") assert.Equal(t, fields["msg"], "omg it is!") assert.Equal(t, fields["context"], "eating raw fish") assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry") }
func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields map[string]string)) { var buffer bytes.Buffer logger := New() logger.Out = &buffer logger.Formatter = &TextFormatter{ DisableColors: true, } log(logger) fields := make(map[string]string) for _, kv := range strings.Split(buffer.String(), " ") { if !strings.Contains(kv, "=") { continue } kvArr := strings.Split(kv, "=") key := strings.TrimSpace(kvArr[0]) val := kvArr[1] if kvArr[1][0] == '"' { var err error val, err = strconv.Unquote(val) assert.NoError(t, err) } fields[key] = val } assertions(fields) }
func TestEncodeOnlyData(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Data: "junk\n\njk\nid:fake", }) assert.NoError(t, err) assert.Equal(t, w.String(), "data: junk\\n\\njk\\nid:fake\n\n") }
func TestEncodeFloat(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Event: "Float", Data: 1.5, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: Float\ndata: 1.5\n\n") }
func TestEncodeWithId(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Id: "t\n:<>\r\test", Data: "junk\n\njk\nid:fa\rke", }) assert.NoError(t, err) assert.Equal(t, w.String(), "id: t\\n:<>\\r\test\ndata: junk\\n\\njk\\nid:fa\\rke\n\n") }
func TestEncodeWithRetry(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Retry: 11, Data: "junk\n\njk\nid:fake\n", }) assert.NoError(t, err) assert.Equal(t, w.String(), "retry: 11\ndata: junk\\n\\njk\\nid:fake\\n\n\n") }
func TestEncodeSlice(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Event: "a slice", Data: []interface{}{1, "text", map[string]interface{}{"foo": "bar"}}, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: a slice\ndata: [1,\"text\",{\"foo\":\"bar\"}]\n\n") }
func TestEncodeInteger(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Event: "an integer", Data: 1, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: an integer\ndata: 1\n\n") }
func TestValidationDisabled(t *testing.T) { backup := Validator Validator = nil defer func() { Validator = backup }() var obj FooStruct req := requestWithBody("POST", "/", `{"bar": "foo"}`) err := JSON.Bind(req, &obj) assert.NoError(t, err) }
func TestEncodeWithEverything(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Event: "abc", Id: "12345", Retry: 10, Data: "some data", }) assert.NoError(t, err) assert.Equal(t, w.String(), "id: 12345\nevent: abc\nretry: 10\ndata: some data\n\n") }
func TestResponseWriterWrite(t *testing.T) { testWritter := httptest.NewRecorder() writer := &responseWriter{} writer.reset(testWritter) w := ResponseWriter(writer) n, err := w.Write([]byte("hola")) assert.Equal(t, n, 4) assert.Equal(t, w.Size(), 4) assert.Equal(t, w.Status(), 200) assert.Equal(t, testWritter.Code, 200) assert.Equal(t, testWritter.Body.String(), "hola") assert.NoError(t, err) n, err = w.Write([]byte(" adios")) assert.Equal(t, n, 6) assert.Equal(t, w.Size(), 10) assert.Equal(t, testWritter.Body.String(), "hola adios") assert.NoError(t, err) }
func TestRenderXML(t *testing.T) { w := httptest.NewRecorder() data := xmlmap{ "foo": "bar", } err := (XML{data}).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>") assert.Equal(t, w.Header().Get("Content-Type"), "application/xml; charset=utf-8") }
func TestRenderJSON(t *testing.T) { w := httptest.NewRecorder() data := map[string]interface{}{ "foo": "bar", } err := (JSON{data}).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n") assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8") }
func TestRenderString(t *testing.T) { w := httptest.NewRecorder() err := (String{ Format: "hola %s %d", Data: []interface{}{"manu", 2}, }).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "hola manu 2") assert.Equal(t, w.Header().Get("Content-Type"), "text/plain; charset=utf-8") }
func TestEncodeMap(t *testing.T) { w := new(bytes.Buffer) err := Encode(w, Event{ Event: "a map", Data: map[string]interface{}{ "foo": "b\n\rar", "bar": "id: 2", }, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: a map\ndata: {\"bar\":\"id: 2\",\"foo\":\"b\\n\\rar\"}\n\n") }
func TestEncodeStruct(t *testing.T) { myStruct := struct { A int B string `json:"value"` }{1, "number"} w := new(bytes.Buffer) err := Encode(w, Event{ Event: "a struct", Data: myStruct, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: a struct\ndata: {\"A\":1,\"value\":\"number\"}\n\n") w.Reset() err = Encode(w, Event{ Event: "a struct", Data: &myStruct, }) assert.NoError(t, err) assert.Equal(t, w.String(), "event: a struct\ndata: {\"A\":1,\"value\":\"number\"}\n\n") }
func TestRenderData(t *testing.T) { w := httptest.NewRecorder() data := []byte("#!PNG some raw data") err := (Data{ ContentType: "image/png", Data: data, }).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "#!PNG some raw data") assert.Equal(t, w.Header().Get("Content-Type"), "image/png") }
func TestRenderSSE(t *testing.T) { w := httptest.NewRecorder() err := (Event{ Event: "msg", Data: "hi! how are you?", }).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "event: msg\ndata: hi! how are you?\n\n") assert.Equal(t, w.Header().Get("Content-Type"), "text/event-stream") assert.Equal(t, w.Header().Get("Cache-Control"), "no-cache") }
func testBodyBinding(t *testing.T, b Binding, name, path, badPath, body, badBody string) { assert.Equal(t, b.Name(), name) obj := FooStruct{} req := requestWithBody("POST", path, body) err := b.Bind(req, &obj) assert.NoError(t, err) assert.Equal(t, obj.Foo, "bar") obj = FooStruct{} req = requestWithBody("POST", badPath, badBody) err = JSON.Bind(req, &obj) assert.Error(t, err) }
func TestRenderHTMLTemplate(t *testing.T) { w := httptest.NewRecorder() templ := template.Must(template.New("t").Parse(`Hello {{.name}}`)) htmlRender := HTMLProduction{Template: templ} instance := htmlRender.Instance("t", map[string]interface{}{ "name": "alexandernyquist", }) err := instance.Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "Hello alexandernyquist") assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8") }
func TestValidateSlice(t *testing.T) { var obj MyObjects var obj2 Object var nu = 10 assert.NoError(t, validate(obj)) assert.NoError(t, validate(&obj)) assert.NoError(t, validate(obj2)) assert.NoError(t, validate(&obj2)) assert.NoError(t, validate(nu)) assert.NoError(t, validate(&nu)) }
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) { b := Form assert.Equal(t, b.Name(), "form") obj := FooBarStruct{} req := requestWithBody(method, path, body) if method == "POST" { req.Header.Add("Content-Type", MIMEPOSTForm) } err := b.Bind(req, &obj) assert.NoError(t, err) assert.Equal(t, obj.Foo, "bar") assert.Equal(t, obj.Bar, "foo") obj = FooBarStruct{} req = requestWithBody(method, badPath, badBody) err = JSON.Bind(req, &obj) assert.Error(t, err) }