Skip to content
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

Update http.send test to work w/o internet access #946

Merged
Merged
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
29 changes: 25 additions & 4 deletions topdown/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func TestInvalidKeyError(t *testing.T) {
// TestHTTPRedirectDisable tests redirects are not enabled by default
func TestHTTPRedirectDisable(t *testing.T) {

// test server
baseURL, teardown := getTestServer()
defer teardown()

// expected result
expectedResult := make(map[string]interface{})
expectedResult["status"] = "301 Moved Permanently"
Expand All @@ -235,10 +239,9 @@ func TestHTTPRedirectDisable(t *testing.T) {
panic(err)
}

var testURL = "http://google.com"
data := loadSmallTestData()
rule := []string{fmt.Sprintf(
`p = x { http.send({"method": "get", "url": "%s"}, x) }`, testURL)}
`p = x { http.send({"method": "get", "url": "%s"}, x) }`, baseURL)}

// run the test
runTopDownTestCase(t, data, "http.send", rule, resultObj.String())
Expand All @@ -248,6 +251,10 @@ func TestHTTPRedirectDisable(t *testing.T) {
// TestHTTPRedirectEnable tests redirects are enabled
func TestHTTPRedirectEnable(t *testing.T) {

// test server
baseURL, teardown := getTestServer()
defer teardown()

// expected result
expectedResult := make(map[string]interface{})
expectedResult["status"] = "200 OK"
Expand All @@ -259,11 +266,25 @@ func TestHTTPRedirectEnable(t *testing.T) {
panic(err)
}

var testURL = "http://google.com"
data := loadSmallTestData()
rule := []string{fmt.Sprintf(
`p = x { http.send({"method": "get", "url": "%s", "enable_redirect": true}, x) }`, testURL)}
`p = x { http.send({"method": "get", "url": "%s", "enable_redirect": true}, x) }`, baseURL)}

// run the test
runTopDownTestCase(t, data, "http.send", rule, resultObj.String())
}

func getTestServer() (baseURL string, teardownFn func()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] There's no need to name the return arguments here, I think. (As long as we avoid "naked returns", it doesn't matter much, though.

mux := http.NewServeMux()
ts := httptest.NewServer(mux)

mux.HandleFunc("/test", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
})

mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/test", http.StatusMovedPermanently)
})

return ts.URL, ts.Close
}