|
| 1 | +package middlewares_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/orsinium-labs/josh" |
| 11 | + "github.com/orsinium-labs/josh/middlewares" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAuth(t *testing.T) { |
| 15 | + var req josh.Req |
| 16 | + url := "http://example.com/foo" |
| 17 | + |
| 18 | + req = httptest.NewRequest("GET", url, nil) |
| 19 | + checkAuth(t, req, 401) |
| 20 | + |
| 21 | + req = httptest.NewRequest("GET", url, nil) |
| 22 | + req.Header.Add("Authorization", "secret") |
| 23 | + checkAuth(t, req, 401) |
| 24 | + |
| 25 | + req = httptest.NewRequest("GET", url, nil) |
| 26 | + req.Header.Add("Authorization", "Bearer ohno") |
| 27 | + checkAuth(t, req, 401) |
| 28 | + |
| 29 | + req = httptest.NewRequest("GET", url, nil) |
| 30 | + req.Header.Add("Authorization", "Bearer secret") |
| 31 | + checkAuth(t, req, 200) |
| 32 | + |
| 33 | + req = httptest.NewRequest("GET", url, nil) |
| 34 | + req.Header.Add("Sec-WebSocket-Protocol", "secret") |
| 35 | + checkAuth(t, req, 401) |
| 36 | + |
| 37 | + req = httptest.NewRequest("GET", url, nil) |
| 38 | + req.Header.Add("Sec-WebSocket-Protocol", "secret, Authorization") |
| 39 | + checkAuth(t, req, 401) |
| 40 | + |
| 41 | + req = httptest.NewRequest("GET", url, nil) |
| 42 | + req.Header.Add("Sec-WebSocket-Protocol", "Authorization, ohno") |
| 43 | + checkAuth(t, req, 401) |
| 44 | + |
| 45 | + req = httptest.NewRequest("GET", url, nil) |
| 46 | + req.Header.Add("Sec-WebSocket-Protocol", "Authorization, secret") |
| 47 | + checkAuth(t, req, 200) |
| 48 | + |
| 49 | + req = httptest.NewRequest("GET", url, nil) |
| 50 | + req.Header.Add("Sec-WebSocket-Protocol", "Authorization") |
| 51 | + req.Header.Add("Sec-WebSocket-Protocol", "secret") |
| 52 | + checkAuth(t, req, 200) |
| 53 | + |
| 54 | + req = httptest.NewRequest("GET", url, nil) |
| 55 | + req.Header.Add("Sec-WebSocket-Protocol", "secret") |
| 56 | + req.Header.Add("Sec-WebSocket-Protocol", "Authorization") |
| 57 | + checkAuth(t, req, 401) |
| 58 | + |
| 59 | + req = httptest.NewRequest("GET", url, nil) |
| 60 | + req.Header.Add("Sec-WebSocket-Protocol", "Authorization") |
| 61 | + req.Header.Add("Sec-WebSocket-Protocol", "ohno") |
| 62 | + req.Header.Add("Sec-WebSocket-Protocol", "secret") |
| 63 | + checkAuth(t, req, 401) |
| 64 | +} |
| 65 | + |
| 66 | +func checkAuth(t *testing.T, req *http.Request, code int) { |
| 67 | + t.Helper() |
| 68 | + type User string |
| 69 | + h := func(r josh.Req) josh.Resp { |
| 70 | + u, err := josh.GetSingleton[User](r) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + if u != "Aragorn" { |
| 75 | + t.Fatalf("got %s, expected Aragorn", u) |
| 76 | + } |
| 77 | + return josh.Ok("all is good") |
| 78 | + } |
| 79 | + v := func(token string) (User, error) { |
| 80 | + if token == "secret" { |
| 81 | + return "Aragorn", nil |
| 82 | + } |
| 83 | + return "", fmt.Errorf("bad token: %s", token) |
| 84 | + } |
| 85 | + h = middlewares.Auth(v, h) |
| 86 | + hh := josh.Wrap(h) |
| 87 | + |
| 88 | + w := httptest.NewRecorder() |
| 89 | + hh(w, req) |
| 90 | + resp := w.Result() |
| 91 | + body, err := io.ReadAll(resp.Body) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("read body: %v", err) |
| 94 | + } |
| 95 | + t.Log(string(body)) |
| 96 | + if resp.StatusCode != code { |
| 97 | + t.Fatalf("got %d, expected %d", resp.StatusCode, code) |
| 98 | + } |
| 99 | +} |
0 commit comments