| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package httpx
- import (
- "net/http"
- "strings"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/tal-tech/go-zero/core/logx"
- )
- type message struct {
- Name string `json:"name"`
- }
- func init() {
- logx.Disable()
- }
- func TestOkJson(t *testing.T) {
- w := tracedResponseWriter{
- headers: make(map[string][]string),
- }
- msg := message{Name: "anyone"}
- OkJson(&w, msg)
- assert.Equal(t, http.StatusOK, w.code)
- assert.Equal(t, "{\"name\":\"anyone\"}", w.builder.String())
- }
- func TestWriteJsonTimeout(t *testing.T) {
- // only log it and ignore
- w := tracedResponseWriter{
- headers: make(map[string][]string),
- timeout: true,
- }
- msg := message{Name: "anyone"}
- WriteJson(&w, http.StatusOK, msg)
- assert.Equal(t, http.StatusOK, w.code)
- }
- func TestWriteJsonLessWritten(t *testing.T) {
- w := tracedResponseWriter{
- headers: make(map[string][]string),
- lessWritten: true,
- }
- msg := message{Name: "anyone"}
- WriteJson(&w, http.StatusOK, msg)
- assert.Equal(t, http.StatusOK, w.code)
- }
- type tracedResponseWriter struct {
- headers map[string][]string
- builder strings.Builder
- code int
- lessWritten bool
- timeout bool
- }
- func (w *tracedResponseWriter) Header() http.Header {
- return w.headers
- }
- func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
- if w.timeout {
- return 0, http.ErrHandlerTimeout
- }
- n, err = w.builder.Write(bytes)
- if w.lessWritten {
- n -= 1
- }
- return
- }
- func (w *tracedResponseWriter) WriteHeader(code int) {
- w.code = code
- }
|