|
| 1 | +// Copyright (c) 2019, The Decred developers |
| 2 | +// See LICENSE for details. |
| 3 | + |
| 4 | +package explorer |
| 5 | + |
| 6 | +import ( |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMenuFormParser(t *testing.T) { |
| 16 | + // Dummy hander for use with the MenuFormParser middleware. |
| 17 | + blah := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | + w.WriteHeader(http.StatusOK) |
| 19 | + io.WriteString(w, "blah") |
| 20 | + }) |
| 21 | + // The handler wrapping blah with MenuFormParser. |
| 22 | + handler := MenuFormParser(blah) |
| 23 | + |
| 24 | + // TEST with no form data. Expected response body: "blah" |
| 25 | + r := httptest.NewRequest("POST", "/set", nil) |
| 26 | + |
| 27 | + w := httptest.NewRecorder() |
| 28 | + handler.ServeHTTP(w, r) |
| 29 | + //resp := w.Result() |
| 30 | + |
| 31 | + if w.Body.String() != "blah" { |
| 32 | + t.Errorf("MenuFormParser failed to call dummy handler's ServeHTTP.") |
| 33 | + } |
| 34 | + |
| 35 | + // TEST with form with requestURIFormKey ("requestURI"). Expect 302 redirect |
| 36 | + // to the relative URL (path, no scheme, no host, no query, no fragment). |
| 37 | + form := url.Values{} |
| 38 | + form.Add(darkModeFormKey, "1") |
| 39 | + form.Add(requestURIFormKey, "https://explorer.decred.org/blocks?junk=1#fraggle") |
| 40 | + |
| 41 | + r = httptest.NewRequest("POST", "/set", strings.NewReader(form.Encode())) |
| 42 | + r.Header.Add("Content-Type", "application/x-www-form-urlencoded") // Required! |
| 43 | + // darkCookie := &http.Cookie{ |
| 44 | + // Name: darkModeCoookie, |
| 45 | + // Value: "1", |
| 46 | + // MaxAge: 0, |
| 47 | + // } |
| 48 | + // r.AddCookie(darkCookie) |
| 49 | + |
| 50 | + w = httptest.NewRecorder() |
| 51 | + handler.ServeHTTP(w, r) |
| 52 | + resp := w.Result() |
| 53 | + |
| 54 | + if w.Body.String() != "" { |
| 55 | + t.Errorf("MenuFormParser failed to respond with an empty body. Got: %v.", |
| 56 | + w.Body.String()) |
| 57 | + } |
| 58 | + |
| 59 | + if resp.StatusCode != http.StatusFound { |
| 60 | + t.Errorf("MenuFormParser failed to respond with status 302 FOUND. Got: %v.", |
| 61 | + resp.StatusCode) |
| 62 | + } |
| 63 | + |
| 64 | + loc, err := resp.Location() |
| 65 | + if err != nil { |
| 66 | + t.Errorf(`Location header not found or invalid: "%v"`, err) |
| 67 | + } |
| 68 | + if loc.String() != "/blocks" { |
| 69 | + t.Errorf(`Location header not set to "/blocks", got "%s".`, loc) |
| 70 | + } |
| 71 | + if loc.IsAbs() { |
| 72 | + t.Errorf(`Location should have been relative, was absolute: "%s"`, loc) |
| 73 | + } |
| 74 | + if len(loc.Query()) > 0 { |
| 75 | + t.Errorf(`Location included a query, should have been an escaped path. Loc: "%s"`, loc) |
| 76 | + } |
| 77 | + if loc.String() != loc.EscapedPath() { |
| 78 | + t.Errorf(`Location should have been JUST an escaped path, got "%s"`, loc) |
| 79 | + } |
| 80 | + if loc.Host != "" { |
| 81 | + t.Errorf("Location should not have a host, but it was %s", loc.Host) |
| 82 | + } |
| 83 | +} |
0 commit comments