Browse Source

registry: use http.NewServeMux for tests instead of gorilla/mux

We don't need gorilla/mux for these tests, so let's use stdlib.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 years ago
parent
commit
72ea8cfb43
1 changed files with 14 additions and 7 deletions
  1. 14 7
      registry/registry_mock_test.go

+ 14 - 7
registry/registry_mock_test.go

@@ -10,7 +10,6 @@ import (
 	"testing"
 
 	"github.com/docker/docker/api/types/registry"
-	"github.com/gorilla/mux"
 	"github.com/sirupsen/logrus"
 	"gotest.tools/v3/assert"
 )
@@ -21,14 +20,14 @@ var (
 )
 
 func init() {
-	r := mux.NewRouter()
+	r := http.NewServeMux()
 
 	// /v1/
-	r.HandleFunc("/v1/_ping", handlerGetPing).Methods(http.MethodGet)
-	r.HandleFunc("/v1/search", handlerSearch).Methods(http.MethodGet)
+	r.HandleFunc("/v1/_ping", handlerGetPing)
+	r.HandleFunc("/v1/search", handlerSearch)
 
 	// /v2/
-	r.HandleFunc("/v2/version", handlerGetPing).Methods(http.MethodGet)
+	r.HandleFunc("/v2/version", handlerGetPing)
 
 	testHTTPServer = httptest.NewServer(handlerAccessLog(r))
 	testHTTPSServer = httptest.NewTLSServer(handlerAccessLog(r))
@@ -123,17 +122,25 @@ func writeResponse(w http.ResponseWriter, message interface{}, code int) {
 	w.WriteHeader(code)
 	body, err := json.Marshal(message)
 	if err != nil {
-		io.WriteString(w, err.Error())
+		_, _ = io.WriteString(w, err.Error())
 		return
 	}
-	w.Write(body)
+	_, _ = w.Write(body)
 }
 
 func handlerGetPing(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodGet {
+		writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+		return
+	}
 	writeResponse(w, true, http.StatusOK)
 }
 
 func handlerSearch(w http.ResponseWriter, r *http.Request) {
+	if r.Method != http.MethodGet {
+		writeResponse(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+		return
+	}
 	result := &registry.SearchResults{
 		Query:      "fakequery",
 		NumResults: 1,