demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
13 hours, 39 minutes ago
History
sites_test.go · 1.6 KB · 53 lines
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestSitesProxy(t *testing.T) { |
| 11 | var sawCookie, sawAuth string |
| 12 | obj := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | sawCookie, sawAuth = r.Header.Get("Cookie"), r.Header.Get("Authorization") |
| 14 | if r.URL.Path == "/s/site" { |
| 15 | http.Redirect(w, r, "http://"+r.Host+"/s/site/index", http.StatusFound) |
| 16 | return |
| 17 | } |
| 18 | w.Header().Set("Set-Cookie", "os=1") |
| 19 | io.WriteString(w, "page "+r.URL.Path) |
| 20 | })) |
| 21 | defer obj.Close() |
| 22 | h, err := sitesProxy(obj.URL) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | get := func(method, path string) *httptest.ResponseRecorder { |
| 27 | r := httptest.NewRequest(method, path, nil) |
| 28 | r.Header.Set("Cookie", "nc_session=secret") |
| 29 | r.Header.Set("Authorization", "Bearer x") |
| 30 | w := httptest.NewRecorder() |
| 31 | h.ServeHTTP(w, r) |
| 32 | return w |
| 33 | } |
| 34 | w := get("GET", "/s/site/index") |
| 35 | if w.Code != 200 || w.Body.String() != "page /s/site/index" { |
| 36 | t.Fatalf("page: %d %q", w.Code, w.Body.String()) |
| 37 | } |
| 38 | if sawCookie != "" || sawAuth != "" { |
| 39 | t.Fatalf("credentials passed on: cookie %q auth %q", sawCookie, sawAuth) |
| 40 | } |
| 41 | if w.Header().Get("Set-Cookie") != "" { |
| 42 | t.Fatal("the object server's cookie reached the visitor") |
| 43 | } |
| 44 | if w := get("GET", "/s/site"); w.Header().Get("Location") != "/s/site/index" { |
| 45 | t.Fatalf("redirect: %q", w.Header().Get("Location")) |
| 46 | } |
| 47 | if w := get("POST", "/s/site/index"); w.Code != http.StatusMethodNotAllowed { |
| 48 | t.Fatalf("POST: %d", w.Code) |
| 49 | } |
| 50 | if w := get("GET", "/api/records"); w.Code != http.StatusNotFound { |
| 51 | t.Fatalf("outside /s/: %d", w.Code) |
| 52 | } |
| 53 | } |