oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs_test
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/docs/internal/docs"
11)
12
13func TestRootRendersAPage(t *testing.T) {
14 app, err := docs.NewForTest(t)
15 if err != nil {
16 t.Fatalf("NewForTest: %v", err)
17 }
18 cookies := signInCookies(t, app, "reader@example.com")
19 req := httptest.NewRequest(http.MethodGet, "/", nil)
20 for _, c := range cookies {
21 req.AddCookie(c)
22 }
23 rec := httptest.NewRecorder()
24 app.Handler().ServeHTTP(rec, req)
25
26 if rec.Code != http.StatusOK {
27 t.Fatalf("GET /: status %d, want 200", rec.Code)
28 }
29 if ct := rec.Header().Get("Content-Type"); ct == "" {
30 t.Error("no Content-Type on the response")
31 }
32}
33
34// TestNewRefusesWhitespaceOrigin is fix round 1's own regression test
35// at the New boundary (origin_test.go covers validateOrigin directly,
36// package-internal): $DOCS_ORIGIN=" " is non-empty, so a bare
37// cfg.Origin == "" check would let it through New and boot an app
38// that 403s every request forever instead of refusing to start. New
39// trims before testing for blank, so whitespace-only collapses onto
40// the same "empty origin" refusal an actually-empty Origin gets —
41// still a startup refusal either way, which is the property that
42// matters here, not which of the two error messages it is.
43func TestNewRefusesWhitespaceOrigin(t *testing.T) {
44 _, err := docs.New(docs.Config{
45 DBPath: filepath.Join(t.TempDir(), "docs.db"),
46 Env: "production",
47 Origin: " ",
48 })
49 if err == nil {
50 t.Fatal("New must refuse a whitespace-only Origin")
51 }
52 if !strings.Contains(err.Error(), "empty") {
53 t.Errorf("error should say the origin is empty: %v", err)
54 }
55}
56
57// TestNewRefusesMalformedOrigin covers the other ways a non-blank
58// Origin can still be unusable — no scheme, no host, or a URL
59// carrying more than scheme://host — at the same New boundary.
60func TestNewRefusesMalformedOrigin(t *testing.T) {
61 for _, origin := range []string{
62 "not a url at all",
63 "localhost:8080",
64 "http://example.com/path",
65 } {
66 t.Run(origin, func(t *testing.T) {
67 _, err := docs.New(docs.Config{
68 DBPath: filepath.Join(t.TempDir(), "docs.db"),
69 Env: "production",
70 Origin: origin,
71 })
72 if err == nil {
73 t.Fatalf("New must refuse malformed origin %q", origin)
74 }
75 })
76 }
77}
78