Skip to content

Commit 447fcb7

Browse files
authored
Merge pull request #14 from github/fail-unexpected-http-requests
Fail the tests if any unexpected HTTP request is made.
2 parents ce3c0e2 + 6e42851 commit 447fcb7

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

internal/pull/pull_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"io/ioutil"
66
"net/http"
7-
"net/http/httptest"
87
"testing"
98

109
"github.com/github/codeql-action-sync/internal/cachedirectory"
@@ -19,15 +18,6 @@ import (
1918
const initialActionRepository = "./pull_test/codeql-action-initial.git"
2019
const modifiedActionRepository = "./pull_test/codeql-action-modified.git"
2120

22-
func getTestGitHubServer(t *testing.T) (*http.ServeMux, string) {
23-
mux := http.NewServeMux()
24-
server := httptest.NewServer(mux)
25-
t.Cleanup(func() {
26-
server.Close()
27-
})
28-
return mux, server.URL
29-
}
30-
3121
func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService {
3222
cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory)
3323
var githubDotComClient *github.Client
@@ -131,7 +121,7 @@ func TestFindRelevantReleases(t *testing.T) {
131121

132122
func TestPullReleases(t *testing.T) {
133123
temporaryDirectory := test.CreateTemporaryDirectory(t)
134-
githubTestServer, githubURL := getTestGitHubServer(t)
124+
githubTestServer, githubURL := test.GetTestHTTPServer(t)
135125
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
136126
test.ServeHTTPResponseFromFile(t, 200, "./pull_test/api/release-some-codeql-version-on-main.json", response)
137127
})
@@ -158,7 +148,7 @@ func TestPullReleases(t *testing.T) {
158148
// If we pull again, we should only download assets where the size mismatches.
159149
err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644)
160150
require.NoError(t, err)
161-
githubTestServer, githubURL = getTestGitHubServer(t)
151+
githubTestServer, githubURL = test.GetTestHTTPServer(t)
162152
githubTestServer.HandleFunc("/api/v3/repos/github/codeql-action/releases/tags/some-codeql-version-on-main", func(response http.ResponseWriter, request *http.Request) {
163153
test.ServeHTTPResponseFromFile(t, 200, "./pull_test/api/release-some-codeql-version-on-main.json", response)
164154
})

test/test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test
33
import (
44
"io/ioutil"
55
"net/http"
6+
"net/http/httptest"
67
"os"
78
"testing"
89

@@ -18,6 +19,18 @@ func CreateTemporaryDirectory(t *testing.T) string {
1819
return directory
1920
}
2021

22+
func GetTestHTTPServer(t *testing.T) (*http.ServeMux, string) {
23+
mux := http.NewServeMux()
24+
mux.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
25+
require.Failf(t, "Unexpected HTTP request: %s %s", request.Method, request.URL.Path)
26+
})
27+
server := httptest.NewServer(mux)
28+
t.Cleanup(func() {
29+
server.Close()
30+
})
31+
return mux, server.URL
32+
}
33+
2134
func ServeHTTPResponseFromFile(t *testing.T, statusCode int, path string, response http.ResponseWriter) {
2235
data, err := ioutil.ReadFile(path)
2336
require.NoError(t, err)

0 commit comments

Comments
 (0)