171 lines
4.1 KiB
Go
171 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type fakeWellKnownGetter struct {
|
|
reg wkRegistry
|
|
}
|
|
|
|
func (f *fakeWellKnownGetter) GetData(ctx context.Context) (wkRegistry, error) {
|
|
return f.reg, nil
|
|
}
|
|
|
|
func Test_GetServer_JSON(t *testing.T) {
|
|
wks := &fakeWellKnownGetter{
|
|
reg: wkRegistry{
|
|
"test": map[string]interface{}{
|
|
"data": map[string]interface{}{
|
|
"key": "value",
|
|
},
|
|
"format": "json",
|
|
},
|
|
"empty": map[string]interface{}{
|
|
"data": map[string]interface{}{},
|
|
"format": "json",
|
|
},
|
|
"level1/level2": map[string]interface{}{
|
|
"data": map[string]interface{}{
|
|
"nestedKey": "nestedValue",
|
|
},
|
|
"format": "json",
|
|
},
|
|
},
|
|
}
|
|
|
|
tt := []struct {
|
|
name string
|
|
path string
|
|
expectedBody string
|
|
expectedCT string
|
|
code int
|
|
}{
|
|
{
|
|
name: "existing",
|
|
path: "/.well-known/test",
|
|
expectedBody: `{"key":"value"}`,
|
|
expectedCT: "application/json",
|
|
code: http.StatusOK,
|
|
},
|
|
}
|
|
|
|
server := GetServer(wks)
|
|
for _, tc := range tt {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", tc.path, nil)
|
|
w := httptest.NewRecorder()
|
|
server.ServeHTTP(w, req)
|
|
|
|
if w.Code != tc.code {
|
|
t.Errorf("Expected status code %d, got %d", tc.code, w.Code)
|
|
}
|
|
|
|
if strings.TrimSpace(w.Body.String()) != tc.expectedBody {
|
|
t.Errorf("Expected body %q, got %q", tc.expectedBody, w.Body.String())
|
|
}
|
|
|
|
if ct := w.Header().Get("Content-Type"); ct != tc.expectedCT {
|
|
t.Errorf("Expected Content-Type %s, got %s", tc.expectedCT, ct)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_GetServer_PlainText_Expanded(t *testing.T) {
|
|
wks := &fakeWellKnownGetter{
|
|
reg: wkRegistry{
|
|
"security.txt": map[string]interface{}{
|
|
"data": []string{
|
|
"Contact: mailto:security@example.com",
|
|
"Contact: mailto:security%2Buri%2Bencoded@example.com",
|
|
"Contact: tel:+1-201-555-0123",
|
|
"Contact: https://example.com/security-contact.html",
|
|
"This is just another comment",
|
|
},
|
|
"format": "text",
|
|
},
|
|
"empty.txt": map[string]interface{}{
|
|
"data": []string{},
|
|
"format": "text",
|
|
},
|
|
"largefile.txt": map[string]interface{}{
|
|
"data": []string{
|
|
"Line 1",
|
|
"Line 2",
|
|
"Line 3",
|
|
"Line 4",
|
|
"Line 5",
|
|
},
|
|
"format": "text",
|
|
},
|
|
},
|
|
}
|
|
|
|
tt := []struct {
|
|
name string
|
|
path string
|
|
expectedBody string
|
|
expectedCT string
|
|
code int
|
|
}{
|
|
{
|
|
name: "plaintext well-known file",
|
|
path: "/.well-known/security.txt",
|
|
expectedBody: `Contact: mailto:security@example.com
|
|
Contact: mailto:security%2Buri%2Bencoded@example.com
|
|
Contact: tel:+1-201-555-0123
|
|
Contact: https://example.com/security-contact.html
|
|
This is just another comment`,
|
|
expectedCT: "text/plain; charset=utf-8",
|
|
code: http.StatusOK,
|
|
},
|
|
{
|
|
name: "empty plaintext file",
|
|
path: "/.well-known/empty.txt",
|
|
expectedBody: "",
|
|
expectedCT: "text/plain; charset=utf-8",
|
|
code: http.StatusOK,
|
|
},
|
|
{
|
|
name: "large plaintext file",
|
|
path: "/.well-known/largefile.txt",
|
|
expectedBody: "Line 1\nLine 2\nLine 3\nLine 4\nLine 5", // Expected content for large files.
|
|
expectedCT: "text/plain; charset=utf-8",
|
|
code: http.StatusOK,
|
|
},
|
|
{
|
|
name: "non-existing plaintext file",
|
|
path: "/.well-known/non-existing.txt",
|
|
expectedBody: "Not found\n",
|
|
expectedCT: "text/plain; charset=utf-8",
|
|
code: http.StatusNotFound,
|
|
},
|
|
}
|
|
|
|
server := GetServer(wks)
|
|
for _, tc := range tt {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequest("GET", tc.path, nil)
|
|
req.Header.Set("Accept", "text/plain")
|
|
w := httptest.NewRecorder()
|
|
server.ServeHTTP(w, req)
|
|
|
|
if w.Code != tc.code {
|
|
t.Errorf("Expected status code %d, got %d", tc.code, w.Code)
|
|
}
|
|
|
|
if w.Body.String() != tc.expectedBody {
|
|
t.Errorf("Expected body %q, got %q", tc.expectedBody, w.Body.String())
|
|
}
|
|
|
|
if ct := w.Header().Get("Content-Type"); ct != tc.expectedCT {
|
|
t.Errorf("Expected Content-Type %s, got %s", tc.expectedCT, ct)
|
|
}
|
|
})
|
|
}
|
|
}
|