aboutsummaryrefslogtreecommitdiffstats
path: root/swarm
diff options
context:
space:
mode:
authorElad <theman@elad.im>2018-08-07 19:40:38 +0800
committerBalint Gabor <balint.g@gmail.com>2018-08-07 19:40:38 +0800
commit4bb2dc3d09a76025292b36ac48f6fffa5450adb8 (patch)
tree6301f44e60de5bee10d72df82773c581bf135b1b /swarm
parentcf05ef9106779da0df62c0c03312fc489171aaa5 (diff)
downloadgo-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar.gz
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar.bz2
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar.lz
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar.xz
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.tar.zst
go-tangerine-4bb2dc3d09a76025292b36ac48f6fffa5450adb8.zip
swarm/api/http: test fixes (#17334)
Diffstat (limited to 'swarm')
-rw-r--r--swarm/api/http/response.go19
-rw-r--r--swarm/api/http/server_test.go51
2 files changed, 21 insertions, 49 deletions
diff --git a/swarm/api/http/response.go b/swarm/api/http/response.go
index 32c09b1f5..9f4788d35 100644
--- a/swarm/api/http/response.go
+++ b/swarm/api/http/response.go
@@ -29,14 +29,12 @@ import (
"github.com/ethereum/go-ethereum/swarm/api"
)
-//metrics variables
var (
htmlCounter = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil)
jsonCounter = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil)
plaintextCounter = metrics.NewRegisteredCounter("api.http.errorpage.plaintext.count", nil)
)
-//parameters needed for formatting the correct HTML page
type ResponseParams struct {
Msg template.HTML
Code int
@@ -45,12 +43,12 @@ type ResponseParams struct {
Details template.HTML
}
-//ShowMultipeChoices is used when a user requests a resource in a manifest which results
-//in ambiguous results. It returns a HTML page with clickable links of each of the entry
-//in the manifest which fits the request URI ambiguity.
-//For example, if the user requests bzz:/<hash>/read and that manifest contains entries
-//"readme.md" and "readinglist.txt", a HTML page is returned with this two links.
-//This only applies if the manifest has no default entry
+// ShowMultipleChoices is used when a user requests a resource in a manifest which results
+// in ambiguous results. It returns a HTML page with clickable links of each of the entry
+// in the manifest which fits the request URI ambiguity.
+// For example, if the user requests bzz:/<hash>/read and that manifest contains entries
+// "readme.md" and "readinglist.txt", a HTML page is returned with this two links.
+// This only applies if the manifest has no default entry
func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.ManifestList) {
log.Debug("ShowMultipleChoices", "ruid", GetRUID(r.Context()), "uri", GetURI(r.Context()))
msg := ""
@@ -66,7 +64,6 @@ func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.Manife
}
uri.Scheme = "bzz-list"
- //request the same url just with bzz-list
msg += fmt.Sprintf("Disambiguation:<br/>Your request may refer to multiple choices.<br/>Click <a class=\"orange\" href='"+"/"+uri.String()+"'>here</a> if your browser does not redirect you within 5 seconds.<script>setTimeout(\"location.href='%s';\",5000);</script><br/>", "/"+uri.String())
RespondTemplate(w, r, "error", msg, http.StatusMultipleChoices)
}
@@ -86,7 +83,6 @@ func RespondError(w http.ResponseWriter, r *http.Request, msg string, code int)
RespondTemplate(w, r, "error", msg, code)
}
-//evaluate if client accepts html or json response
func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
w.WriteHeader(params.Code)
@@ -108,7 +104,6 @@ func respond(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
}
}
-//return a HTML page
func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams) {
htmlCounter.Inc(1)
log.Debug("respondHTML", "ruid", GetRUID(r.Context()))
@@ -118,7 +113,6 @@ func respondHTML(w http.ResponseWriter, r *http.Request, params *ResponseParams)
}
}
-//return JSON
func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
jsonCounter.Inc(1)
log.Debug("respondJSON", "ruid", GetRUID(r.Context()))
@@ -126,7 +120,6 @@ func respondJSON(w http.ResponseWriter, r *http.Request, params *ResponseParams)
return json.NewEncoder(w).Encode(params)
}
-//return plaintext
func respondPlaintext(w http.ResponseWriter, r *http.Request, params *ResponseParams) error {
plaintextCounter.Inc(1)
log.Debug("respondPlaintext", "ruid", GetRUID(r.Context()))
diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go
index f23f236b2..3ac60596b 100644
--- a/swarm/api/http/server_test.go
+++ b/swarm/api/http/server_test.go
@@ -909,7 +909,6 @@ func TestMethodsNotAllowed(t *testing.T) {
}
-// HTTP convenience function
func httpDo(httpMethod string, url string, reqBody io.Reader, headers map[string]string, verbose bool, t *testing.T) (*http.Response, string) {
// Build the Request
req, err := http.NewRequest(httpMethod, url, reqBody)
@@ -942,11 +941,10 @@ func httpDo(httpMethod string, url string, reqBody io.Reader, headers map[string
}
func TestGet(t *testing.T) {
- // Setup Swarm
srv := testutil.NewTestSwarmServer(t, serverFunc)
defer srv.Close()
- testCases := []struct {
+ for _, testCase := range []struct {
uri string
method string
headers map[string]string
@@ -955,25 +953,22 @@ func TestGet(t *testing.T) {
verbose bool
}{
{
- // Accept: text/html GET / -> 200 HTML, Swarm Landing Page
uri: fmt.Sprintf("%s/", srv.URL),
method: "GET",
headers: map[string]string{"Accept": "text/html"},
expectedStatusCode: 200,
- assertResponseBody: "<a href=\"/bzz:/theswarm.eth\">Swarm</a>: Serverless Hosting Incentivised peer-to-peer Storage and Content Distribution",
+ assertResponseBody: "Swarm: Serverless Hosting Incentivised Peer-To-Peer Storage And Content Distribution",
verbose: false,
},
{
- // Accept: application/json GET / -> 200 'Welcome to Swarm'
uri: fmt.Sprintf("%s/", srv.URL),
method: "GET",
headers: map[string]string{"Accept": "application/json"},
expectedStatusCode: 200,
- assertResponseBody: "Welcome to Swarm!",
+ assertResponseBody: "Swarm: Please request a valid ENS or swarm hash with the appropriate bzz scheme",
verbose: false,
},
{
- // GET /robots.txt -> 200
uri: fmt.Sprintf("%s/robots.txt", srv.URL),
method: "GET",
headers: map[string]string{"Accept": "text/html"},
@@ -982,62 +977,54 @@ func TestGet(t *testing.T) {
verbose: false,
},
{
- // GET /path_that_doesnt exist -> 400
uri: fmt.Sprintf("%s/nonexistent_path", srv.URL),
method: "GET",
headers: map[string]string{},
- expectedStatusCode: 400,
+ expectedStatusCode: 404,
verbose: false,
},
{
- // GET bzz-invalid:/ -> 400
uri: fmt.Sprintf("%s/bzz:asdf/", srv.URL),
method: "GET",
headers: map[string]string{},
- expectedStatusCode: 400,
+ expectedStatusCode: 404,
verbose: false,
},
{
- // GET bzz-invalid:/ -> 400
uri: fmt.Sprintf("%s/tbz2/", srv.URL),
method: "GET",
headers: map[string]string{},
- expectedStatusCode: 400,
+ expectedStatusCode: 404,
verbose: false,
},
{
- // GET bzz-invalid:/ -> 400
uri: fmt.Sprintf("%s/bzz-rack:/", srv.URL),
method: "GET",
headers: map[string]string{},
- expectedStatusCode: 400,
+ expectedStatusCode: 404,
verbose: false,
},
{
- // GET bzz-invalid:/ -> 400
uri: fmt.Sprintf("%s/bzz-ls", srv.URL),
method: "GET",
headers: map[string]string{},
- expectedStatusCode: 400,
+ expectedStatusCode: 404,
verbose: false,
},
- }
-
- for _, testCase := range testCases {
+ } {
t.Run("GET "+testCase.uri, func(t *testing.T) {
res, body := httpDo(testCase.method, testCase.uri, nil, testCase.headers, testCase.verbose, t)
if res.StatusCode != testCase.expectedStatusCode {
- t.Fatalf("expected %s %s to return a %v but it didn't", testCase.method, testCase.uri, testCase.expectedStatusCode)
+ t.Fatalf("expected status code %d but got %d", testCase.expectedStatusCode, res.StatusCode)
}
if testCase.assertResponseBody != "" && !strings.Contains(body, testCase.assertResponseBody) {
- t.Fatalf("expected %s %s to have %s within HTTP response body but it didn't", testCase.method, testCase.uri, testCase.assertResponseBody)
+ t.Fatalf("expected response to be: %s but got: %s", testCase.assertResponseBody, body)
}
})
}
}
func TestModify(t *testing.T) {
- // Setup Swarm and upload a test file to it
srv := testutil.NewTestSwarmServer(t, serverFunc)
defer srv.Close()
@@ -1057,7 +1044,7 @@ func TestModify(t *testing.T) {
t.Fatal(err)
}
- testCases := []struct {
+ for _, testCase := range []struct {
uri string
method string
headers map[string]string
@@ -1068,7 +1055,6 @@ func TestModify(t *testing.T) {
verbose bool
}{
{
- // DELETE bzz:/hash -> 200 OK
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
method: "DELETE",
headers: map[string]string{},
@@ -1077,7 +1063,6 @@ func TestModify(t *testing.T) {
verbose: false,
},
{
- // PUT bzz:/hash -> 405 Method Not Allowed
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
method: "PUT",
headers: map[string]string{},
@@ -1085,7 +1070,6 @@ func TestModify(t *testing.T) {
verbose: false,
},
{
- // PUT bzz-raw:/hash -> 405 Method Not Allowed
uri: fmt.Sprintf("%s/bzz-raw:/%s", srv.URL, hash),
method: "PUT",
headers: map[string]string{},
@@ -1093,7 +1077,6 @@ func TestModify(t *testing.T) {
verbose: false,
},
{
- // PATCH bzz:/hash -> 405 Method Not Allowed
uri: fmt.Sprintf("%s/bzz:/%s", srv.URL, hash),
method: "PATCH",
headers: map[string]string{},
@@ -1101,7 +1084,6 @@ func TestModify(t *testing.T) {
verbose: false,
},
{
- // POST bzz-raw:/ -> 200 OK
uri: fmt.Sprintf("%s/bzz-raw:/", srv.URL),
method: "POST",
headers: map[string]string{},
@@ -1111,7 +1093,6 @@ func TestModify(t *testing.T) {
verbose: false,
},
{
- // POST bzz-raw:/encrypt -> 200 OK
uri: fmt.Sprintf("%s/bzz-raw:/encrypt", srv.URL),
method: "POST",
headers: map[string]string{},
@@ -1120,19 +1101,17 @@ func TestModify(t *testing.T) {
assertResponseHeaders: map[string]string{"Content-Length": "128"},
verbose: false,
},
- }
-
- for _, testCase := range testCases {
+ } {
t.Run(testCase.method+" "+testCase.uri, func(t *testing.T) {
reqBody := bytes.NewReader(testCase.requestBody)
res, body := httpDo(testCase.method, testCase.uri, reqBody, testCase.headers, testCase.verbose, t)
if res.StatusCode != testCase.expectedStatusCode {
- t.Fatalf("expected %s %s to return a %v but it returned a %v instead", testCase.method, testCase.uri, testCase.expectedStatusCode, res.StatusCode)
+ t.Fatalf("expected status code %d but got %d", testCase.expectedStatusCode, res.StatusCode)
}
if testCase.assertResponseBody != "" && !strings.Contains(body, testCase.assertResponseBody) {
t.Log(body)
- t.Fatalf("expected %s %s to have %s within HTTP response body but it didn't", testCase.method, testCase.uri, testCase.assertResponseBody)
+ t.Fatalf("expected response %s but got %s", testCase.assertResponseBody, body)
}
for key, value := range testCase.assertResponseHeaders {
if res.Header.Get(key) != value {