Skip to content

Fail the tests if any unexpected HTTP request is made. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2020
Merged
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
14 changes: 2 additions & 12 deletions internal/pull/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

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

func getTestGitHubServer(t *testing.T) (*http.ServeMux, string) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(func() {
server.Close()
})
return mux, server.URL
}

func getTestPullService(t *testing.T, temporaryDirectory string, gitCloneURL string, githubURL string) pullService {
cacheDirectory := cachedirectory.NewCacheDirectory(temporaryDirectory)
var githubDotComClient *github.Client
Expand Down Expand Up @@ -131,7 +121,7 @@ func TestFindRelevantReleases(t *testing.T) {

func TestPullReleases(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubURL := getTestGitHubServer(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, 200, "./pull_test/api/release-some-codeql-version-on-main.json", response)
})
Expand All @@ -158,7 +148,7 @@ func TestPullReleases(t *testing.T) {
// If we pull again, we should only download assets where the size mismatches.
err = ioutil.WriteFile(pullService.cacheDirectory.AssetPath("some-codeql-version-on-v1-and-v2", "codeql-bundle.tar.gz"), []byte("Some nonsense."), 0644)
require.NoError(t, err)
githubTestServer, githubURL = getTestGitHubServer(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, 200, "./pull_test/api/release-some-codeql-version-on-main.json", response)
})
Expand Down
13 changes: 13 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

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

func GetTestHTTPServer(t *testing.T) (*http.ServeMux, string) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
require.Failf(t, "Unexpected HTTP request: %s %s", request.Method, request.URL.Path)
})
server := httptest.NewServer(mux)
t.Cleanup(func() {
server.Close()
})
return mux, server.URL
}

func ServeHTTPResponseFromFile(t *testing.T, statusCode int, path string, response http.ResponseWriter) {
data, err := ioutil.ReadFile(path)
require.NoError(t, err)
Expand Down