|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.uber.org/mock/gomock" |
| 9 | +) |
| 10 | + |
| 11 | +func TestJiaozifsResponse(t *testing.T) { |
| 12 | + t.Run("not found", func(t *testing.T) { |
| 13 | + ctrl := gomock.NewController(t) |
| 14 | + resp := NewMockResponseWriter(ctrl) |
| 15 | + jzResp := JiaozifsResponse{resp} |
| 16 | + |
| 17 | + resp.EXPECT().WriteHeader(http.StatusNotFound) |
| 18 | + jzResp.NotFound() |
| 19 | + }) |
| 20 | + |
| 21 | + t.Run("ok", func(t *testing.T) { |
| 22 | + ctrl := gomock.NewController(t) |
| 23 | + resp := NewMockResponseWriter(ctrl) |
| 24 | + jzResp := JiaozifsResponse{resp} |
| 25 | + |
| 26 | + resp.EXPECT().WriteHeader(http.StatusOK) |
| 27 | + jzResp.OK() |
| 28 | + }) |
| 29 | + t.Run("code", func(t *testing.T) { |
| 30 | + ctrl := gomock.NewController(t) |
| 31 | + resp := NewMockResponseWriter(ctrl) |
| 32 | + jzResp := JiaozifsResponse{resp} |
| 33 | + |
| 34 | + resp.EXPECT().WriteHeader(http.StatusCreated) |
| 35 | + jzResp.Code(http.StatusCreated) |
| 36 | + }) |
| 37 | + t.Run("error", func(t *testing.T) { |
| 38 | + ctrl := gomock.NewController(t) |
| 39 | + resp := NewMockResponseWriter(ctrl) |
| 40 | + jzResp := JiaozifsResponse{resp} |
| 41 | + |
| 42 | + resp.EXPECT().WriteHeader(http.StatusInternalServerError) |
| 43 | + resp.EXPECT().Write([]byte("mock")) |
| 44 | + jzResp.Error(fmt.Errorf("mock")) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("string", func(t *testing.T) { |
| 48 | + ctrl := gomock.NewController(t) |
| 49 | + resp := NewMockResponseWriter(ctrl) |
| 50 | + jzResp := JiaozifsResponse{resp} |
| 51 | + |
| 52 | + resp.EXPECT().WriteHeader(http.StatusOK) |
| 53 | + resp.EXPECT().Header().DoAndReturn(func() http.Header { |
| 54 | + return make(http.Header) |
| 55 | + }) |
| 56 | + resp.EXPECT().Write([]byte("test")) |
| 57 | + jzResp.String("test") |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("string with code", func(t *testing.T) { |
| 61 | + ctrl := gomock.NewController(t) |
| 62 | + resp := NewMockResponseWriter(ctrl) |
| 63 | + jzResp := JiaozifsResponse{resp} |
| 64 | + |
| 65 | + resp.EXPECT().WriteHeader(http.StatusCreated) |
| 66 | + resp.EXPECT().Header().DoAndReturn(func() http.Header { |
| 67 | + return make(http.Header) |
| 68 | + }) |
| 69 | + resp.EXPECT().Write([]byte("test")) |
| 70 | + jzResp.String("test", http.StatusCreated) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("json", func(t *testing.T) { |
| 74 | + ctrl := gomock.NewController(t) |
| 75 | + resp := NewMockResponseWriter(ctrl) |
| 76 | + jzResp := JiaozifsResponse{resp} |
| 77 | + |
| 78 | + resp.EXPECT().WriteHeader(http.StatusOK) |
| 79 | + resp.EXPECT().Header().DoAndReturn(func() http.Header { |
| 80 | + return make(http.Header) |
| 81 | + }) |
| 82 | + |
| 83 | + resp.EXPECT().Write([]byte("{\"Name\":\"aa\"}\n")) |
| 84 | + jzResp.JSON(struct { |
| 85 | + Name string |
| 86 | + }{Name: "aa"}) |
| 87 | + }) |
| 88 | + t.Run("json with code", func(t *testing.T) { |
| 89 | + ctrl := gomock.NewController(t) |
| 90 | + resp := NewMockResponseWriter(ctrl) |
| 91 | + jzResp := JiaozifsResponse{resp} |
| 92 | + |
| 93 | + resp.EXPECT().WriteHeader(http.StatusCreated) |
| 94 | + resp.EXPECT().Header().DoAndReturn(func() http.Header { |
| 95 | + return make(http.Header) |
| 96 | + }) |
| 97 | + |
| 98 | + resp.EXPECT().Write([]byte("{\"Name\":\"aa\"}\n")) |
| 99 | + jzResp.JSON(struct { |
| 100 | + Name string |
| 101 | + }{Name: "aa"}, http.StatusCreated) |
| 102 | + }) |
| 103 | + |
| 104 | +} |
0 commit comments