Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions pkg/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ func NewAPIServer(a *APIHandler, p int, is bool, c, k string) *APIServer {

// Serve launches the API Server.
func (a *APIServer) Serve() {
mux := http.NewServeMux()
mux.Handle(apiPathConfig, a.handler)

mcs := &http.Server{
Addr: fmt.Sprintf(":%v", a.port),
Handler: mux,
Handler: a.handler,
}

glog.Info("launching server")
Expand Down Expand Up @@ -80,14 +77,14 @@ func NewServerAPIHandler(s Server) *APIHandler {
// ServeHTTP handles the requests for the machine config server
// API handler.
func (sh *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

if r.URL.Path == "" {
w.WriteHeader(http.StatusBadRequest)
base, pool := path.Split(r.URL.Path)
if base != apiPathConfig {
w.WriteHeader(http.StatusNotFound)
return
}

cr := poolRequest{
machinePool: path.Base(r.URL.Path),
machinePool: pool,
}

conf, err := sh.server.GetConfig(cr)
Expand Down
33 changes: 17 additions & 16 deletions pkg/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,50 @@ func (ms *mockServer) GetConfig(pr poolRequest) (*ignv2_2types.Config, error) {
}

type scenario struct {
name string
request *http.Request
expectedStatus int
serverFunc func(poolRequest) (*ignv2_2types.Config, error)
}

func TestAPIHandler(t *testing.T) {
scenarios := []scenario{
{
name: "not-found",
request: httptest.NewRequest("GET", "http://testrequest/does-not-exist", nil),
expectedStatus: http.StatusNotFound,
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
return nil, nil
},
},
{
name: "internal-server",
request: httptest.NewRequest("GET", "http://testrequest/config/does-not-exist", nil),
expectedStatus: http.StatusInternalServerError,
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), fmt.Errorf("not acceptable")
},
},
{
name: "success",
request: httptest.NewRequest("GET", "http://testrequest/config/master", nil),
expectedStatus: http.StatusOK,
serverFunc: func(poolRequest) (*ignv2_2types.Config, error) {
return new(ignv2_2types.Config), nil
},
},
}

for i := range scenarios {
req := httptest.NewRequest("GET", "http://testrequest/", nil)
w := httptest.NewRecorder()
ms := &mockServer{
GetConfigFn: scenarios[i].serverFunc,
}
handler := NewServerAPIHandler(ms)
handler.ServeHTTP(w, req)
for _, scenario := range scenarios {
t.Run(scenario.request.URL.Path, func(t *testing.T) {
w := httptest.NewRecorder()
ms := &mockServer{
GetConfigFn: scenario.serverFunc,
}
handler := NewServerAPIHandler(ms)
handler.ServeHTTP(w, scenario.request)

resp := w.Result()
resp := w.Result()

if resp.StatusCode != scenarios[i].expectedStatus {
t.Errorf("API Handler test failed for: %s, expected: %d, received: %d", scenarios[i].name, scenarios[i].expectedStatus, resp.StatusCode)
}
if resp.StatusCode != scenario.expectedStatus {
t.Errorf("API Handler test failed: expected: %d, received: %d", scenario.expectedStatus, resp.StatusCode)
}
})
}
}