From dba6c26997a67b5243b3f451b0316db3ccbdec69 Mon Sep 17 00:00:00 2001 From: Chris Gavin Date: Mon, 24 Aug 2020 09:59:05 +0100 Subject: [PATCH] Use the Gorilla Mux router for better routing of mocked GitHub APIs. --- go.mod | 1 + go.sum | 2 ++ internal/pull/pull_test.go | 17 +++++++---------- test/test.go | 5 +++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 03cba41..94e14df 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/go-git/go-git/v5 v5.1.0 github.com/google/go-github/v32 v32.1.0 + github.com/gorilla/mux v1.8.0 github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e github.com/pkg/errors v0.8.1 github.com/spf13/cobra v1.0.0 diff --git a/go.sum b/go.sum index 346b284..32c7f34 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/google/go-github/v32 v32.1.0 h1:GWkQOdXqviCPx7Q7Fj+KyPoGm4SwHRh8rheoP github.com/google/go-github/v32 v32.1.0/go.mod h1:rIEpZD9CTDQwDK9GDrtMTycQNA4JU3qBsCizh3q2WCI= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= diff --git a/internal/pull/pull_test.go b/internal/pull/pull_test.go index 3ab3acd..595b482 100644 --- a/internal/pull/pull_test.go +++ b/internal/pull/pull_test.go @@ -124,18 +124,16 @@ func TestPullReleases(t *testing.T) { githubTestServer, githubURL := test.GetTestHTTPServer(t) githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response) - }) + }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/1", func(response http.ResponseWriter, request *http.Request) { - require.Equal(t, "application/octet-stream", request.Header.Get("accept")) test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-main.bin", response) - }) + }).Methods("GET").Headers("accept", "application/octet-stream") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response) - }) + }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) { - require.Equal(t, "application/octet-stream", request.Header.Get("accept")) test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response) - }) + }).Methods("GET").Headers("accept", "application/octet-stream") pullService := getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL) err := pullService.pullGit(true) require.NoError(t, err) @@ -151,14 +149,13 @@ func TestPullReleases(t *testing.T) { githubTestServer, githubURL = test.GetTestHTTPServer(t) githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-main.json", response) - }) + }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-v1-and-v2", func(response http.ResponseWriter, request *http.Request) { test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/release-some-codeql-version-on-v1-and-v2.json", response) - }) + }).Methods("GET") githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/assets/2", func(response http.ResponseWriter, request *http.Request) { - require.Equal(t, "application/octet-stream", request.Header.Get("accept")) test.ServeHTTPResponseFromFile(t, http.StatusOK, "./pull_test/api/asset-some-codeql-version-on-v1-and-v2.bin", response) - }) + }).Methods("GET").Headers("accept", "application/octet-stream") pullService = getTestPullService(t, temporaryDirectory, initialActionRepository, githubURL) err = pullService.pullReleases() require.NoError(t, err) diff --git a/test/test.go b/test/test.go index d633d58..edb4d80 100644 --- a/test/test.go +++ b/test/test.go @@ -7,6 +7,7 @@ import ( "os" "testing" + "github.com/gorilla/mux" "github.com/stretchr/testify/require" ) @@ -19,8 +20,8 @@ func CreateTemporaryDirectory(t *testing.T) string { return directory } -func GetTestHTTPServer(t *testing.T) (*http.ServeMux, string) { - mux := http.NewServeMux() +func GetTestHTTPServer(t *testing.T) (*mux.Router, string) { + mux := mux.NewRouter() mux.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) { require.Failf(t, "Unexpected HTTP request: %s %s", request.Method, request.URL.Path) })