Skip to content

Commit 0718a01

Browse files
committed
test: refactor regression tests and enhance assertions
1 parent 287383c commit 0718a01

File tree

8 files changed

+42
-56
lines changed

8 files changed

+42
-56
lines changed

pkg/internal/regressiontests/http_auth_with_ts_regression_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//go:build regressiontest
2-
// +build regressiontest
32

43
package regressiontests
54

65
import (
76
"net/http"
7+
"net/url"
88
"testing"
99

1010
"github.com/bsv-blockchain/go-bsv-middleware/pkg/internal/regressiontests/internal/typescript"
@@ -20,19 +20,19 @@ func TestAuthMiddlewareAuthenticatesTypescriptClient(t *testing.T) {
2020
headers map[string]string
2121
}{
2222
"get request": {
23-
method: "GET",
23+
method: http.MethodGet,
2424
query: "",
2525
body: nil,
2626
headers: nil,
2727
},
2828
"get request with query params": {
29-
method: "GET",
29+
method: http.MethodGet,
3030
query: "test=123&other=abc",
3131
body: nil,
3232
headers: nil,
3333
},
3434
"get request with headers": {
35-
method: "GET",
35+
method: http.MethodGet,
3636
query: "",
3737
body: nil,
3838
headers: map[string]string{
@@ -42,7 +42,7 @@ func TestAuthMiddlewareAuthenticatesTypescriptClient(t *testing.T) {
4242
},
4343
},
4444
"post request": {
45-
method: "POST",
45+
method: http.MethodPost,
4646
query: "",
4747
body: map[string]string{
4848
"test": "123",
@@ -53,6 +53,10 @@ func TestAuthMiddlewareAuthenticatesTypescriptClient(t *testing.T) {
5353
"Content-Type": "application/json",
5454
},
5555
},
56+
"invalid query params": {
57+
method: http.MethodPost,
58+
query: "shirts size",
59+
},
5660
}
5761
for name, test := range testCases {
5862
t.Run(name, func(t *testing.T) {
@@ -69,10 +73,10 @@ func TestAuthMiddlewareAuthenticatesTypescriptClient(t *testing.T) {
6973
then.Request(r).
7074
HasMethod(test.method).
7175
HasHeadersContaining(test.headers).
72-
HasQueryMatching(test.query).
76+
HasQueryMatching(url.PathEscape(test.query)).
7377
HasBodyMatching(test.body)
7478

75-
// TODO check indentity key
79+
// TODO check identity key
7680

7781
_, err := w.Write([]byte("Pong!"))
7882
require.NoError(t, err)
@@ -84,15 +88,15 @@ func TestAuthMiddlewareAuthenticatesTypescriptClient(t *testing.T) {
8488
url.Path = "/ping"
8589
url.RawQuery = test.query
8690

87-
response, err := typescript.AuthFetch(t,
91+
response := typescript.AuthFetch(t,
8892
url.String(),
8993
typescript.WithMethod(test.method),
9094
typescript.WithHeaders(test.headers),
9195
typescript.WithBody(test.body),
9296
)
9397

9498
// then:
95-
then.Response(response).WithNoError(err).
99+
then.Response(response).
96100
HasStatus(200).
97101
HasHeader("x-bsv-auth-identity-key").
98102
HasBody("Pong!")

pkg/internal/regressiontests/internal/typescript/typescript_auth_fetch_caller.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package typescript
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/http"
67
"os/exec"
78
"path/filepath"
89
"runtime"
@@ -69,15 +70,6 @@ func WithHeaders(headers map[string]string) func(*Options) {
6970
}
7071
}
7172

72-
func WithHeader(key string, value string) func(*Options) {
73-
return func(options *Options) {
74-
if options.headers == nil {
75-
options.headers = make([]string, 0)
76-
}
77-
options.headers = append(options.headers, key+":"+value)
78-
}
79-
}
80-
8173
var validMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
8274

8375
const resultIndicator = "==================RESULT==============="
@@ -89,10 +81,10 @@ type AuthFetchResponse struct {
8981
Body string `json:"body"`
9082
}
9183

92-
func AuthFetch(t testing.TB, url string, opts ...func(*Options)) (*AuthFetchResponse, error) {
84+
func AuthFetch(t testing.TB, url string, opts ...func(*Options)) *AuthFetchResponse {
9385
options := to.OptionsWithDefault(Options{
9486
url: url,
95-
method: "GET",
87+
method: http.MethodGet,
9688
}, opts...)
9789

9890
command, args := authFetchCommand()
@@ -116,15 +108,13 @@ func AuthFetch(t testing.TB, url string, opts ...func(*Options)) (*AuthFetchResp
116108

117109
t.Log(log)
118110

119-
if err != nil {
120-
return nil, fmt.Errorf("AuthFetch failed: %w, %s", err, result)
121-
}
111+
require.NoErrorf(t, err, "Failed to run auth fetch command\n%s", output)
122112

123113
var response AuthFetchResponse
124114
err = json.Unmarshal([]byte(result), &response)
125115
require.NoErrorf(t, err, "Result from js script must be json with response\n%s", result)
126116

127-
return &response, nil
117+
return &response
128118
}
129119

130120
func authFetchCommand() (string, []string) {

pkg/internal/regressiontests/internal/typescript/typescript_auth_fetch_callet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestCheckTypescriptAuthFetchCaller(t *testing.T) {
1212
t.Skip("used for auth fetch caller development purposes")
1313

14-
response, err := typescript.AuthFetch(t, "http://localhost:8100")
14+
response := typescript.AuthFetch(t, "http://localhost:8100")
1515
assert.NoError(t, err)
1616
require.NotNil(t, response)
1717
assert.NotEmpty(t, response.Status)

pkg/internal/regressiontests/testabilities/assertion_regression.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/bsv-blockchain/go-bsv-middleware/pkg/internal/regressiontests/internal/typescript"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
type RegressionTestAssertion interface {
@@ -28,6 +29,9 @@ func (a *regressionTestAssertion) Request(request *http.Request) RequestAssertio
2829
}
2930

3031
func (a *regressionTestAssertion) Response(response *typescript.AuthFetchResponse) AuthFetchResponseAssertion {
32+
a.Helper()
33+
require.NotNil(a, response, "response should not be nil")
34+
3135
return &authFetchResponseAssertion{
3236
TB: a,
3337
response: response,

pkg/internal/regressiontests/testabilities/assertion_request.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ type requestAssertion struct {
2525
}
2626

2727
func (a *requestAssertion) HasMethod(httpMethod string) RequestAssertion {
28+
a.Helper()
2829
assert.Equalf(a, httpMethod, a.request.Method, "Expect to receive %s request", httpMethod)
2930
return a
3031
}
3132

3233
func (a *requestAssertion) HasHeadersContaining(headers map[string]string) RequestAssertion {
34+
a.Helper()
3335
for headerName, headerValue := range headers {
3436
assert.Equalf(a, headerValue, a.request.Header.Get(headerName), "Header %s value received by handler should match", headerName)
3537
}
@@ -38,11 +40,13 @@ func (a *requestAssertion) HasHeadersContaining(headers map[string]string) Reque
3840
}
3941

4042
func (a *requestAssertion) HasQueryMatching(query string) RequestAssertion {
43+
a.Helper()
4144
assert.Equal(a, query, a.request.URL.RawQuery, "query params received by handler should match")
4245
return a
4346
}
4447

4548
func (a *requestAssertion) HasBodyMatching(expectedBody map[string]string) RequestAssertion {
49+
a.Helper()
4650
bodyBytes, err := io.ReadAll(a.request.Body)
4751
assert.NoError(a, err, "failed to read request body: invalid test setup")
4852
// ensure the body is not closed.

pkg/internal/regressiontests/testabilities/assertion_response.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,33 @@ import (
55

66
"github.com/bsv-blockchain/go-bsv-middleware/pkg/internal/regressiontests/internal/typescript"
77
"github.com/stretchr/testify/assert"
8-
"github.com/stretchr/testify/require"
98
)
109

1110
type AuthFetchResponseAssertion interface {
12-
WithNoError(error) SuccessAuthFetchResponseAssertion
13-
}
14-
15-
type SuccessAuthFetchResponseAssertion interface {
16-
HasStatus(int) SuccessAuthFetchResponseAssertion
17-
HasHeader(string) SuccessAuthFetchResponseAssertion
18-
HasBody(any) SuccessAuthFetchResponseAssertion
11+
HasStatus(int) AuthFetchResponseAssertion
12+
HasHeader(string) AuthFetchResponseAssertion
13+
HasBody(any) AuthFetchResponseAssertion
1914
}
2015

2116
type authFetchResponseAssertion struct {
2217
testing.TB
2318
response *typescript.AuthFetchResponse
2419
}
2520

26-
func (a *authFetchResponseAssertion) WithNoError(err error) SuccessAuthFetchResponseAssertion {
27-
assert.NoError(a, err, "fetch should result with no error")
28-
require.NotNil(a, a.response, "fetch should return a response")
29-
return a
30-
}
31-
32-
func (a *authFetchResponseAssertion) HasStatus(status int) SuccessAuthFetchResponseAssertion {
21+
func (a *authFetchResponseAssertion) HasStatus(status int) AuthFetchResponseAssertion {
22+
a.Helper()
3323
assert.Equal(a, status, a.response.Status, "fetch should return status 200")
3424
return a
3525
}
3626

37-
func (a *authFetchResponseAssertion) HasHeader(headerName string) SuccessAuthFetchResponseAssertion {
38-
assert.Contains(a, a.response.Headers, "x-bsv-auth-identity-key")
27+
func (a *authFetchResponseAssertion) HasHeader(headerName string) AuthFetchResponseAssertion {
28+
a.Helper()
29+
assert.Contains(a, a.response.Headers, headerName)
3930
return a
4031
}
4132

42-
func (a *authFetchResponseAssertion) HasBody(body any) SuccessAuthFetchResponseAssertion {
33+
func (a *authFetchResponseAssertion) HasBody(body any) AuthFetchResponseAssertion {
34+
a.Helper()
4335
assert.Equal(a, a.response.Body, body)
4436
return a
4537
}

pkg/internal/test/mocks/http.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"testing"
1414

1515
"github.com/bsv-blockchain/go-bsv-middleware/pkg/interfaces"
16-
"github.com/bsv-blockchain/go-bsv-middleware/pkg/internal/logging"
1716
middleware "github.com/bsv-blockchain/go-bsv-middleware/pkg/middleware/auth"
1817
"github.com/bsv-blockchain/go-bsv-middleware/pkg/transport"
1918
"github.com/bsv-blockchain/go-sdk/auth"
@@ -292,14 +291,6 @@ func WithLogger(s *MockHTTPServer) *MockHTTPServer {
292291
return s
293292
}
294293

295-
// WithTestLogger wraps a MockHTTPServer with a test logger for capturing log output during tests.
296-
func WithTestLogger(t testing.TB) func(s *MockHTTPServer) *MockHTTPServer {
297-
return func(s *MockHTTPServer) *MockHTTPServer {
298-
s.logger = logging.NewTestLogger(t)
299-
return s
300-
}
301-
}
302-
303294
func prepareAndCallRequest(t *testing.T, method, authURL string, headers map[string]string, jsonData []byte) *http.Response {
304295
req, err := http.NewRequest(method, authURL, bytes.NewBuffer(jsonData))
305296
require.Nil(t, err)

pkg/transport/http/transport.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,12 @@ func buildRequestPayload(req *http.Request, requestID string) ([]byte, error) {
394394
writer.WriteString(req.Method)
395395
writer.WriteOptionalString(req.URL.Path)
396396

397-
seachParams := req.URL.RawQuery
398-
if seachParams != "" {
399-
seachParams = "?" + seachParams
397+
searchParams := req.URL.RawQuery
398+
if searchParams != "" {
399+
// auth client is using query string with leading "?", so the middleware need to include that character also.
400+
searchParams = "?" + searchParams
400401
}
401-
writer.WriteOptionalString(seachParams)
402+
writer.WriteOptionalString(searchParams)
402403

403404
var includedHeaders [][]string
404405
for k, v := range req.Header {

0 commit comments

Comments
 (0)