|
| 1 | +package docs |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestClient_resolveURL(t *testing.T) { |
| 13 | + c := Client{Host: "www.rwx.com", Scheme: "https"} |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + expected string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "bare path", |
| 22 | + input: "/docs/rwx/guides/ci", |
| 23 | + expected: "https://www.rwx.com/docs/rwx/guides/ci", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "full URL with https scheme", |
| 27 | + input: "https://www.rwx.com/docs/rwx/guides/ci", |
| 28 | + expected: "https://www.rwx.com/docs/rwx/guides/ci", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "full URL with http scheme", |
| 32 | + input: "http://www.rwx.com/docs/rwx/guides/ci", |
| 33 | + expected: "http://www.rwx.com/docs/rwx/guides/ci", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "host with www but no scheme", |
| 37 | + input: "www.rwx.com/docs/rwx/guides/ci", |
| 38 | + expected: "https://www.rwx.com/docs/rwx/guides/ci", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "host without www or scheme", |
| 42 | + input: "rwx.com/docs/rwx/guides/ci", |
| 43 | + expected: "https://rwx.com/docs/rwx/guides/ci", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + result := c.resolveURL(tt.input) |
| 50 | + require.Equal(t, tt.expected, result) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestClient_resolveURL_customHost(t *testing.T) { |
| 56 | + c := Client{Host: "docs.example.com", Scheme: "http"} |
| 57 | + |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + input string |
| 61 | + expected string |
| 62 | + }{ |
| 63 | + { |
| 64 | + name: "bare path with custom host", |
| 65 | + input: "/some/article", |
| 66 | + expected: "http://docs.example.com/some/article", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "full URL ignores custom host", |
| 70 | + input: "https://other.com/some/article", |
| 71 | + expected: "https://other.com/some/article", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "custom host without scheme", |
| 75 | + input: "docs.example.com/some/article", |
| 76 | + expected: "http://docs.example.com/some/article", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + result := c.resolveURL(tt.input) |
| 83 | + require.Equal(t, tt.expected, result) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestClient_FetchArticle_withFullURL(t *testing.T) { |
| 89 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 90 | + require.Equal(t, "/docs/rwx/guides/ci", r.URL.Path) |
| 91 | + require.Equal(t, "text/markdown", r.Header.Get("Accept")) |
| 92 | + fmt.Fprint(w, "# CI Guide\n\nWelcome.") |
| 93 | + })) |
| 94 | + t.Cleanup(server.Close) |
| 95 | + |
| 96 | + c := Client{Host: server.Listener.Addr().String(), Scheme: "http"} |
| 97 | + |
| 98 | + // Pass a full URL pointing at the test server |
| 99 | + fullURL := fmt.Sprintf("http://%s/docs/rwx/guides/ci", server.Listener.Addr().String()) |
| 100 | + body, err := c.FetchArticle(fullURL) |
| 101 | + require.NoError(t, err) |
| 102 | + require.Equal(t, "# CI Guide\n\nWelcome.", body) |
| 103 | +} |
| 104 | + |
| 105 | +func TestClient_FetchArticle_withPath(t *testing.T) { |
| 106 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 107 | + require.Equal(t, "/docs/rwx/guides/ci", r.URL.Path) |
| 108 | + require.Equal(t, "text/markdown", r.Header.Get("Accept")) |
| 109 | + fmt.Fprint(w, "# CI Guide\n\nWelcome.") |
| 110 | + })) |
| 111 | + t.Cleanup(server.Close) |
| 112 | + |
| 113 | + c := Client{Host: server.Listener.Addr().String(), Scheme: "http"} |
| 114 | + |
| 115 | + body, err := c.FetchArticle("/docs/rwx/guides/ci") |
| 116 | + require.NoError(t, err) |
| 117 | + require.Equal(t, "# CI Guide\n\nWelcome.", body) |
| 118 | +} |
| 119 | + |
| 120 | +func TestClient_FetchArticle_withHostNoScheme(t *testing.T) { |
| 121 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 122 | + require.Equal(t, "/docs/rwx/guides/ci", r.URL.Path) |
| 123 | + fmt.Fprint(w, "# CI Guide\n\nWelcome.") |
| 124 | + })) |
| 125 | + t.Cleanup(server.Close) |
| 126 | + |
| 127 | + c := Client{Host: server.Listener.Addr().String(), Scheme: "http"} |
| 128 | + |
| 129 | + // Pass host-prefixed input (without scheme) |
| 130 | + hostPrefixed := fmt.Sprintf("%s/docs/rwx/guides/ci", server.Listener.Addr().String()) |
| 131 | + body, err := c.FetchArticle(hostPrefixed) |
| 132 | + require.NoError(t, err) |
| 133 | + require.Equal(t, "# CI Guide\n\nWelcome.", body) |
| 134 | +} |
0 commit comments