1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/* Copyright 2017 LinkedIn Corp. Licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package httpserver
import (
"encoding/json"
"net/http"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"testing"
"github.com/spf13/viper"
)
func setupConfiguration() {
viper.Reset()
viper.Set("client-profile.test.client-id", "testid")
viper.Set("storage.teststorage.class-name", "inmemory")
viper.Set("consumer.testconsumer.class-name", "kafka_zk")
viper.Set("consumer.testconsumer.client-profile", "test")
viper.Set("cluster.testcluster.class-name", "kafka")
viper.Set("cluster.testcluster.client-profile", "test")
viper.Set("evaluator.testevaluator.class-name", "caching")
viper.Set("notifier.testnotifier.class-name", "null")
}
func TestHttpServer_configMain(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body - just test that it decodes
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigMain
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
}
func TestHttpServer_configStorageList(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/storage", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigModuleList
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "storage", resp.Coordinator, "Expected Coordinator to be storage, not %v", resp.Coordinator)
assert.Equalf(t, []string{"teststorage"}, resp.Modules, "Expected Modules to be [teststorage], not %v", resp.Modules)
}
func TestHttpServer_configConsumerList(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/consumer", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigModuleList
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "consumer", resp.Coordinator, "Expected Coordinator to be consumer, not %v", resp.Coordinator)
assert.Equalf(t, []string{"testconsumer"}, resp.Modules, "Expected Modules to be [testconsumer], not %v", resp.Modules)
}
func TestHttpServer_configClusterList(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/cluster", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigModuleList
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "cluster", resp.Coordinator, "Expected Coordinator to be cluster, not %v", resp.Coordinator)
assert.Equalf(t, []string{"testcluster"}, resp.Modules, "Expected Modules to be [testcluster], not %v", resp.Modules)
}
func TestHttpServer_configEvaluatorList(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/evaluator", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigModuleList
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "evaluator", resp.Coordinator, "Expected Coordinator to be evaluator, not %v", resp.Coordinator)
assert.Equalf(t, []string{"testevaluator"}, resp.Modules, "Expected Modules to be [testevaluator], not %v", resp.Modules)
}
func TestHttpServer_configNotifierList(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/notifier", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp httpResponseConfigModuleList
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "notifier", resp.Coordinator, "Expected Coordinator to be notifier, not %v", resp.Coordinator)
assert.Equalf(t, []string{"testnotifier"}, resp.Modules, "Expected Modules to be [testnotifier], not %v", resp.Modules)
}
func TestHttpServer_configStorageDetail(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/storage/teststorage", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Need a custom type for the test, due to variations in the response for different module types
type ResponseType struct {
Error bool `json:"error"`
Message string `json:"message"`
Module httpResponseConfigModuleStorage `json:"module"`
Request httpResponseRequestInfo `json:"request"`
}
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp ResponseType
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "inmemory", resp.Module.ClassName, "Expected ClassName to be immemory, not %v", resp.Module.ClassName)
// Call again for a 404
req, err = http.NewRequest("GET", "/v3/config/storage/nomodule", nil)
assert.NoError(t, err, "Expected request setup to return no error")
rr = httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusNotFound, rr.Code, "Expected response code to be 404, not %v", rr.Code)
}
func TestHttpServer_configConsumerDetail(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/consumer/testconsumer", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Need a custom type for the test, due to variations in the response for different module types
type ResponseType struct {
Error bool `json:"error"`
Message string `json:"message"`
Module httpResponseConfigModuleConsumer `json:"module"`
Request httpResponseRequestInfo `json:"request"`
}
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp ResponseType
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "kafka_zk", resp.Module.ClassName, "Expected ClassName to be kafka_zk, not %v", resp.Module.ClassName)
// Call again for a 404
req, err = http.NewRequest("GET", "/v3/config/consumer/nomodule", nil)
assert.NoError(t, err, "Expected request setup to return no error")
rr = httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusNotFound, rr.Code, "Expected response code to be 404, not %v", rr.Code)
}
func TestHttpServer_configEvaluatorDetail(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/evaluator/testevaluator", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Need a custom type for the test, due to variations in the response for different module types
type ResponseType struct {
Error bool `json:"error"`
Message string `json:"message"`
Module httpResponseConfigModuleEvaluator `json:"module"`
Request httpResponseRequestInfo `json:"request"`
}
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp ResponseType
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "caching", resp.Module.ClassName, "Expected ClassName to be caching, not %v", resp.Module.ClassName)
// Call again for a 404
req, err = http.NewRequest("GET", "/v3/config/evaluator/nomodule", nil)
assert.NoError(t, err, "Expected request setup to return no error")
rr = httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusNotFound, rr.Code, "Expected response code to be 404, not %v", rr.Code)
}
func TestHttpServer_configNotifierDetail(t *testing.T) {
coordinator := fixtureConfiguredCoordinator()
setupConfiguration()
// Set up a request
req, err := http.NewRequest("GET", "/v3/config/notifier/testnotifier", nil)
assert.NoError(t, err, "Expected request setup to return no error")
// Call the handler via httprouter
rr := httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusOK, rr.Code, "Expected response code to be 200, not %v", rr.Code)
// Need a custom type for the test, due to variations in the response for different module types
type ResponseType struct {
Error bool `json:"error"`
Message string `json:"message"`
Module httpResponseConfigModuleNotifierNull `json:"module"`
Request httpResponseRequestInfo `json:"request"`
}
// Parse response body
decoder := json.NewDecoder(rr.Body)
var resp ResponseType
err = decoder.Decode(&resp)
assert.NoError(t, err, "Expected body decode to return no error")
assert.False(t, resp.Error, "Expected response Error to be false")
assert.Equalf(t, "null", resp.Module.ClassName, "Expected ClassName to be null, not %v", resp.Module.ClassName)
// Call again for a 404
req, err = http.NewRequest("GET", "/v3/config/notifier/nomodule", nil)
assert.NoError(t, err, "Expected request setup to return no error")
rr = httptest.NewRecorder()
coordinator.router.ServeHTTP(rr, req)
assert.Equalf(t, http.StatusNotFound, rr.Code, "Expected response code to be 404, not %v", rr.Code)
}
|