TestMiddlewareHandlers 测试 Middleware 相关的处理器
(t *testing.T)
| 104 | |
| 105 | // TestMiddlewareHandlers 测试 Middleware 相关的处理器 |
| 106 | func TestMiddlewareHandlers(t *testing.T) { |
| 107 | srv, cleanup := setupTestServer(t) |
| 108 | defer cleanup() |
| 109 | |
| 110 | var middlewareID string |
| 111 | |
| 112 | t.Run("CreateMiddleware", func(t *testing.T) { |
| 113 | body := `{ |
| 114 | "name": "test_middleware", |
| 115 | "type": "custom", |
| 116 | "description": "A test middleware", |
| 117 | "priority": 10 |
| 118 | }` |
| 119 | |
| 120 | req := httptest.NewRequest(http.MethodPost, "/v1/middlewares", strings.NewReader(body)) |
| 121 | req.Header.Set("Content-Type", "application/json") |
| 122 | w := httptest.NewRecorder() |
| 123 | |
| 124 | srv.Router().ServeHTTP(w, req) |
| 125 | |
| 126 | assert.Equal(t, http.StatusCreated, w.Code) |
| 127 | |
| 128 | var resp map[string]any |
| 129 | err := json.Unmarshal(w.Body.Bytes(), &resp) |
| 130 | require.NoError(t, err) |
| 131 | |
| 132 | data := resp["data"].(map[string]any) |
| 133 | middlewareID = data["id"].(string) |
| 134 | assert.NotEmpty(t, middlewareID) |
| 135 | }) |
| 136 | |
| 137 | t.Run("ListMiddlewares", func(t *testing.T) { |
| 138 | req := httptest.NewRequest(http.MethodGet, "/v1/middlewares", nil) |
| 139 | w := httptest.NewRecorder() |
| 140 | |
| 141 | srv.Router().ServeHTTP(w, req) |
| 142 | |
| 143 | assert.Equal(t, http.StatusOK, w.Code) |
| 144 | }) |
| 145 | |
| 146 | t.Run("GetMiddleware", func(t *testing.T) { |
| 147 | req := httptest.NewRequest(http.MethodGet, "/v1/middlewares/"+middlewareID, nil) |
| 148 | w := httptest.NewRecorder() |
| 149 | |
| 150 | srv.Router().ServeHTTP(w, req) |
| 151 | |
| 152 | assert.Equal(t, http.StatusOK, w.Code) |
| 153 | }) |
| 154 | |
| 155 | t.Run("ListMiddlewareRegistry", func(t *testing.T) { |
| 156 | req := httptest.NewRequest(http.MethodGet, "/v1/middlewares/registry", nil) |
| 157 | w := httptest.NewRecorder() |
| 158 | |
| 159 | srv.Router().ServeHTTP(w, req) |
| 160 | |
| 161 | assert.Equal(t, http.StatusOK, w.Code) |
| 162 | // Registry may return different structure |
| 163 | }) |
nothing calls this directly
no test coverage detected