- Run gofmt on user schema, config test, group handler - Remove unused mergeGroupIDs function - Restore shared test helpers (newJSONResponse, queuedHTTPUpstream) that were in deleted Sora test file
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/tlsfingerprint"
|
|
)
|
|
|
|
// queuedHTTPUpstream is a test helper that serves pre-loaded responses in order.
|
|
type queuedHTTPUpstream struct {
|
|
responses []*http.Response
|
|
requests []*http.Request
|
|
tlsFlags []bool
|
|
}
|
|
|
|
func (u *queuedHTTPUpstream) Do(_ *http.Request, _ string, _ int64, _ int) (*http.Response, error) {
|
|
return nil, fmt.Errorf("unexpected Do call")
|
|
}
|
|
|
|
func (u *queuedHTTPUpstream) DoWithTLS(req *http.Request, _ string, _ int64, _ int, profile *tlsfingerprint.Profile) (*http.Response, error) {
|
|
u.requests = append(u.requests, req)
|
|
u.tlsFlags = append(u.tlsFlags, profile != nil)
|
|
if len(u.responses) == 0 {
|
|
return nil, fmt.Errorf("no mocked response")
|
|
}
|
|
resp := u.responses[0]
|
|
u.responses = u.responses[1:]
|
|
return resp, nil
|
|
}
|
|
|
|
// newJSONResponse creates a simple HTTP response for testing.
|
|
func newJSONResponse(status int, body string) *http.Response {
|
|
return &http.Response{
|
|
StatusCode: status,
|
|
Header: make(http.Header),
|
|
Body: io.NopCloser(strings.NewReader(body)),
|
|
}
|
|
}
|
|
|
|
// newJSONResponseWithHeader creates a JSON response with a custom header.
|
|
func newJSONResponseWithHeader(status int, body, key, value string) *http.Response {
|
|
resp := newJSONResponse(status, body)
|
|
resp.Header.Set(key, value)
|
|
return resp
|
|
}
|