Skip to content

Commit

Permalink
Update http.send test to work w/o internet access
Browse files Browse the repository at this point in the history
Fixes open-policy-agent#945

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
  • Loading branch information
ashutosh-narkar committed Sep 14, 2018
1 parent 5f2bba0 commit b923304
Showing 1 changed file with 25 additions and 4 deletions.
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()) {
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
}

0 comments on commit b923304

Please sign in to comment.