-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(server): add comprehensive unit tests for ACME challenge and HTT…
…PS redirect
- Loading branch information
Showing
1 changed file
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/yarlson/zero/internal/cert" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
t.Run("ACME challenge endpoint", func(t *testing.T) { | ||
store := cert.NewStore() | ||
srv := New(store, 8080) | ||
|
||
// Start server in background | ||
go func() { | ||
err := srv.Start() | ||
require.NoError(t, err) | ||
}() | ||
defer srv.Stop() | ||
|
||
// Wait for server to start | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Test cases | ||
tests := []struct { | ||
name string | ||
token string | ||
response string | ||
method string | ||
expectedCode int | ||
expectedBody string | ||
}{ | ||
{ | ||
name: "valid challenge", | ||
token: "valid-token", | ||
response: "challenge-response", | ||
method: http.MethodGet, | ||
expectedCode: http.StatusOK, | ||
expectedBody: "challenge-response", | ||
}, | ||
{ | ||
name: "non-existent token", | ||
token: "nonexistent-token", | ||
method: http.MethodGet, | ||
expectedCode: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "wrong method", | ||
token: "valid-token", | ||
method: http.MethodPost, | ||
expectedCode: http.StatusMethodNotAllowed, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if tc.response != "" { | ||
store.StoreChallenge(tc.token, tc.response) | ||
} | ||
|
||
// Make request | ||
req, err := http.NewRequest(tc.method, | ||
fmt.Sprintf("http://localhost:8080/.well-known/acme-challenge/%s", tc.token), | ||
nil) | ||
require.NoError(t, err) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, tc.expectedCode, resp.StatusCode) | ||
|
||
if tc.expectedBody != "" { | ||
body, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expectedBody, string(body)) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("HTTPS redirect", func(t *testing.T) { | ||
store := cert.NewStore() | ||
srv := New(store, 8081) | ||
|
||
go func() { | ||
err := srv.Start() | ||
require.NoError(t, err) | ||
}() | ||
defer srv.Stop() | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Test cases | ||
tests := []struct { | ||
name string | ||
requestURL string | ||
expectedURL string | ||
}{ | ||
{ | ||
name: "basic redirect", | ||
requestURL: "http://localhost:8081/path", | ||
expectedURL: "https://localhost/path", | ||
}, | ||
{ | ||
name: "redirect with query params", | ||
requestURL: "http://localhost:8081/path?key=value", | ||
expectedURL: "https://localhost/path?key=value", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
client := &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse | ||
}, | ||
} | ||
|
||
resp, err := client.Get(tc.requestURL) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusMovedPermanently, resp.StatusCode) | ||
location := resp.Header.Get("Location") | ||
assert.Equal(t, tc.expectedURL, location) | ||
}) | ||
} | ||
}) | ||
} |