Skip to content

Commit

Permalink
pkg/server/api: Drop the muxer
Browse files Browse the repository at this point in the history
Each sub-handler would be switching on Path anyway, and we don't have
so many paths for the muxer's extra level of path switching to help
much.  And it's nice to only have to put things like [1]'s method
guard in one spot (vs. repeating in each of the muxer's sub-handlers).

[1]: #240
  • Loading branch information
wking committed Dec 17, 2018
1 parent 7b029f8 commit f5b7772
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
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)
}
})
}
}

0 comments on commit f5b7772

Please sign in to comment.