From bc907a5d8c74132c3d60c7e01b1036de529628f8 Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Fri, 10 Jan 2025 10:52:23 +0800 Subject: [PATCH] chore: move testing.http to a single folder (#814) --- testing/{ => http}/assertable_json.go | 23 ++-- testing/{ => http}/assertable_json_test.go | 37 +++--- testing/{ => http}/test_request.go | 29 +++-- testing/{ => http}/test_request_test.go | 15 +-- testing/{ => http}/test_response.go | 29 +++-- testing/{ => http}/test_response_test.go | 125 +++++++++------------ testing/test_case.go | 3 +- 7 files changed, 127 insertions(+), 134 deletions(-) rename testing/{ => http}/assertable_json.go (88%) rename testing/{ => http}/assertable_json_test.go (83%) rename testing/{ => http}/test_request.go (86%) rename testing/{ => http}/test_request_test.go (95%) rename testing/{ => http}/test_response.go (93%) rename testing/{ => http}/test_response_test.go (80%) diff --git a/testing/assertable_json.go b/testing/http/assertable_json.go similarity index 88% rename from testing/assertable_json.go rename to testing/http/assertable_json.go index fd2c8cb3a..d2c68cef6 100644 --- a/testing/assertable_json.go +++ b/testing/http/assertable_json.go @@ -1,4 +1,4 @@ -package testing +package http import ( "fmt" @@ -6,17 +6,19 @@ import ( "github.com/stretchr/testify/assert" + "github.com/goravel/framework/contracts/foundation" contractstesting "github.com/goravel/framework/contracts/testing" "github.com/goravel/framework/support/maps" ) type AssertableJson struct { t *testing.T - json string + json foundation.Json + jsonStr string decoded map[string]any } -func NewAssertableJSON(t *testing.T, jsonStr string) (contractstesting.AssertableJSON, error) { +func NewAssertableJSON(t *testing.T, json foundation.Json, jsonStr string) (contractstesting.AssertableJSON, error) { var decoded map[string]any err := json.Unmarshal([]byte(jsonStr), &decoded) if err != nil { @@ -25,7 +27,8 @@ func NewAssertableJSON(t *testing.T, jsonStr string) (contractstesting.Assertabl return &AssertableJson{ t: t, - json: jsonStr, + json: json, + jsonStr: jsonStr, decoded: decoded, }, nil } @@ -111,9 +114,9 @@ func (r *AssertableJson) First(key string, callback func(contractstesting.Assert } firstItem := array[0] - itemJson, err := json.Marshal(firstItem) + itemJson, err := r.json.Marshal(firstItem) if assert.NoError(r.t, err, "Failed to marshal the first item") { - newJson, err := NewAssertableJSON(r.t, string(itemJson)) + newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson)) if assert.NoError(r.t, err, "Failed to create AssertableJSON for first item") { callback(newJson) } @@ -138,12 +141,12 @@ func (r *AssertableJson) HasWithScope(key string, length int, callback func(cont } if len(array) > 0 { - itemJson, err := json.Marshal(array[0]) + itemJson, err := r.json.Marshal(array[0]) if !assert.NoError(r.t, err, "Failed to marshal the first item of array") { return r } - newJson, err := NewAssertableJSON(r.t, string(itemJson)) + newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson)) if !assert.NoError(r.t, err, "Failed to create AssertableJSON for first item in scoped array") { return r } @@ -166,12 +169,12 @@ func (r *AssertableJson) Each(key string, callback func(contractstesting.Asserta } for _, item := range array { - itemJson, err := json.Marshal(item) + itemJson, err := r.json.Marshal(item) if !assert.NoError(r.t, err) { continue } - newJson, err := NewAssertableJSON(r.t, string(itemJson)) + newJson, err := NewAssertableJSON(r.t, r.json, string(itemJson)) if !assert.NoError(r.t, err) { continue } diff --git a/testing/assertable_json_test.go b/testing/http/assertable_json_test.go similarity index 83% rename from testing/assertable_json_test.go rename to testing/http/assertable_json_test.go index 3b6f1fbb6..8c8983e30 100644 --- a/testing/assertable_json_test.go +++ b/testing/http/assertable_json_test.go @@ -1,4 +1,4 @@ -package testing +package http import ( "testing" @@ -13,35 +13,28 @@ type AssertableJsonTestSuite struct { } func TestAssertableJsonTestSuite(t *testing.T) { - json = &testJson{} - suite.Run(t, new(AssertableJsonTestSuite)) } func (s *AssertableJsonTestSuite) SetupTest() { - -} - -func (s *AssertableJsonTestSuite) TearDownSuite() { - json = nil } func (s *AssertableJsonTestSuite) TestNewAssertableJSON() { validJSON := `{"key1": "value1", "key2": [1, 2, 3]}` invalidJSON := `{"key1": "value1", "key2": [1, 2, 3]` - assertable, err := NewAssertableJSON(s.T(), validJSON) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, validJSON) s.NoError(err) s.NotNil(assertable) - assertable, err = NewAssertableJSON(s.T(), invalidJSON) + assertable, err = NewAssertableJSON(s.T(), &testJson{}, invalidJSON) s.Error(err) s.Nil(assertable) } func (s *AssertableJsonTestSuite) TestCount() { jsonStr := `{"items": [1, 2, 3], "otherKey": "value"}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) assertable.Count("items", 3) @@ -56,7 +49,7 @@ func (s *AssertableJsonTestSuite) TestHas() { "nested": {"deep": "value"}, "nullKey": null }` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) assertable.Has("key1") @@ -67,7 +60,7 @@ func (s *AssertableJsonTestSuite) TestHas() { func (s *AssertableJsonTestSuite) TestHasAll() { jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test all keys exist @@ -79,7 +72,7 @@ func (s *AssertableJsonTestSuite) TestHasAll() { func (s *AssertableJsonTestSuite) TestHasAny() { jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test at least one key exists @@ -91,7 +84,7 @@ func (s *AssertableJsonTestSuite) TestHasAny() { func (s *AssertableJsonTestSuite) TestMissing() { jsonStr := `{"key1": "value1"}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test key is missing @@ -103,7 +96,7 @@ func (s *AssertableJsonTestSuite) TestMissing() { func (s *AssertableJsonTestSuite) TestMissingAll() { jsonStr := `{"key1": "value1"}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test all keys are missing @@ -122,7 +115,7 @@ func (s *AssertableJsonTestSuite) TestWhere() { "objKey": {"nested": "value"}, "arrayKey": [1, 2, 3] }` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test correct value @@ -143,7 +136,7 @@ func (s *AssertableJsonTestSuite) TestWhere() { func (s *AssertableJsonTestSuite) TestWhereNot() { jsonStr := `{"key1": "value1", "key2": [1, 2, 3]}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test value is not as expected @@ -156,7 +149,7 @@ func (s *AssertableJsonTestSuite) TestWhereNot() { func (s *AssertableJsonTestSuite) TestFirst() { jsonStr := `{"items": [{"id": 1}, {"id": 2}]}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test fetching the first item @@ -176,7 +169,7 @@ func (s *AssertableJsonTestSuite) TestFirst() { func (s *AssertableJsonTestSuite) TestHasWithScope() { jsonStr := `{"items": [{"id": 1}, {"id": 2}]}` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test has with correct length @@ -198,7 +191,7 @@ func (s *AssertableJsonTestSuite) TestEach() { "nonArray": "value" }` - assertable, err := NewAssertableJSON(s.T(), jsonStr) + assertable, err := NewAssertableJSON(s.T(), &testJson{}, jsonStr) s.NoError(err) // Test iterating over each item @@ -214,7 +207,7 @@ func (s *AssertableJsonTestSuite) TestEach() { // Test with an empty array emptyJsonStr := `{"items": []}` - emptyAssertable, err := NewAssertableJSON(s.T(), emptyJsonStr) + emptyAssertable, err := NewAssertableJSON(s.T(), &testJson{}, emptyJsonStr) s.NoError(err) emptyCallCount := 0 diff --git a/testing/test_request.go b/testing/http/test_request.go similarity index 86% rename from testing/test_request.go rename to testing/http/test_request.go index 59c3232b5..1318fe7eb 100644 --- a/testing/test_request.go +++ b/testing/http/test_request.go @@ -1,4 +1,4 @@ -package testing +package http import ( "context" @@ -9,6 +9,9 @@ import ( "net/http/httptest" "testing" + "github.com/goravel/framework/contracts/foundation" + "github.com/goravel/framework/contracts/route" + "github.com/goravel/framework/contracts/session" contractstesting "github.com/goravel/framework/contracts/testing" "github.com/goravel/framework/errors" "github.com/goravel/framework/support/collect" @@ -22,15 +25,21 @@ type TestRequest struct { bind any defaultHeaders map[string]string defaultCookies map[string]string + json foundation.Json + route route.Route + session session.Manager sessionAttributes map[string]any } -func NewTestRequest(t *testing.T) contractstesting.TestRequest { +func NewTestRequest(t *testing.T, json foundation.Json, route route.Route, session session.Manager) contractstesting.TestRequest { return &TestRequest{ t: t, ctx: context.Background(), defaultHeaders: make(map[string]string), defaultCookies: make(map[string]string), + json: json, + route: route, + session: session, sessionAttributes: make(map[string]any), } } @@ -153,23 +162,23 @@ func (r *TestRequest) call(method string, uri string, body io.Reader) (contracts req.AddCookie(&cookie) } - if routeFacade == nil { + if r.route == nil { r.t.Fatal(errors.RouteFacadeNotSet.SetModule(errors.ModuleTesting)) } - response, err := routeFacade.Test(req) + response, err := r.route.Test(req) if err != nil { return nil, err } - testResponse := NewTestResponse(r.t, response) + testResponse := NewTestResponse(r.t, response, r.json, r.session) if r.bind != nil { body, err := testResponse.Content() if err != nil { return nil, err } - if err := json.Unmarshal([]byte(body), r.bind); err != nil { + if err := r.json.Unmarshal([]byte(body), r.bind); err != nil { return nil, err } } @@ -182,18 +191,18 @@ func (r *TestRequest) setSession() error { return nil } - if sessionFacade == nil { + if r.session == nil { return errors.SessionFacadeNotSet } // Retrieve session driver - driver, err := sessionFacade.Driver() + driver, err := r.session.Driver() if err != nil { return err } // Build session - session, err := sessionFacade.BuildSession(driver) + session, err := r.session.BuildSession(driver) if err != nil { return err } @@ -209,6 +218,6 @@ func (r *TestRequest) setSession() error { } // Release session - sessionFacade.ReleaseSession(session) + r.session.ReleaseSession(session) return nil } diff --git a/testing/test_request_test.go b/testing/http/test_request_test.go similarity index 95% rename from testing/test_request_test.go rename to testing/http/test_request_test.go index d652a6b9d..999eae2c2 100644 --- a/testing/test_request_test.go +++ b/testing/http/test_request_test.go @@ -1,4 +1,4 @@ -package testing +package http import ( "bytes" @@ -31,24 +31,19 @@ func TestTestRequestSuite(t *testing.T) { func (s *TestRequestSuite) SetupTest() { s.mockRoute = mocksroute.NewRoute(s.T()) s.mockSessionManager = mockssession.NewManager(s.T()) - json = &testJson{} - routeFacade = s.mockRoute - sessionFacade = s.mockSessionManager + s.testRequest = &TestRequest{ t: s.T(), ctx: context.Background(), defaultHeaders: make(map[string]string), defaultCookies: make(map[string]string), sessionAttributes: make(map[string]any), + json: &testJson{}, + route: s.mockRoute, + session: s.mockSessionManager, } } -func (s *TestRequestSuite) TearDownTest() { - json = nil - routeFacade = nil - sessionFacade = nil -} - func (s *TestRequestSuite) TestBindAndCall() { s.mockRoute.EXPECT().Test(httptest.NewRequest("GET", "/", nil).WithContext(context.Background())).Return(&http.Response{ Body: io.NopCloser(bytes.NewBufferString(`{"name": "John", "age": 30}`)), diff --git a/testing/test_response.go b/testing/http/test_response.go similarity index 93% rename from testing/test_response.go rename to testing/http/test_response.go index c9b1daeae..cc519a649 100644 --- a/testing/test_response.go +++ b/testing/http/test_response.go @@ -1,4 +1,4 @@ -package testing +package http import ( "fmt" @@ -12,6 +12,8 @@ import ( "github.com/stretchr/testify/assert" + "github.com/goravel/framework/contracts/foundation" + contractssession "github.com/goravel/framework/contracts/session" contractstesting "github.com/goravel/framework/contracts/testing" "github.com/goravel/framework/errors" "github.com/goravel/framework/support/carbon" @@ -22,11 +24,18 @@ type TestResponseImpl struct { mu sync.Mutex response *http.Response content string + json foundation.Json + session contractssession.Manager sessionAttributes map[string]any } -func NewTestResponse(t *testing.T, response *http.Response) contractstesting.TestResponse { - return &TestResponseImpl{t: t, response: response} +func NewTestResponse(t *testing.T, response *http.Response, json foundation.Json, session contractssession.Manager) contractstesting.TestResponse { + return &TestResponseImpl{ + t: t, + response: response, + json: json, + session: session, + } } func (r *TestResponseImpl) Json() (map[string]any, error) { @@ -35,7 +44,7 @@ func (r *TestResponseImpl) Json() (map[string]any, error) { return nil, err } - testAble, err := NewAssertableJSON(r.t, content) + testAble, err := NewAssertableJSON(r.t, r.json, content) if err != nil { return nil, err } @@ -60,24 +69,24 @@ func (r *TestResponseImpl) Session() (map[string]any, error) { return r.sessionAttributes, nil } - if sessionFacade == nil { + if r.session == nil { return nil, errors.SessionFacadeNotSet } // Retrieve session driver - driver, err := sessionFacade.Driver() + driver, err := r.session.Driver() if err != nil { return nil, err } // Build session - session, err := sessionFacade.BuildSession(driver) + session, err := r.session.BuildSession(driver) if err != nil { return nil, err } r.sessionAttributes = session.All() - sessionFacade.ReleaseSession(session) + r.session.ReleaseSession(session) return r.sessionAttributes, nil } @@ -352,7 +361,7 @@ func (r *TestResponseImpl) AssertJson(data map[string]any) contractstesting.Test content, err := r.getContent() assert.Nil(r.t, err) - assertableJson, err := NewAssertableJSON(r.t, content) + assertableJson, err := NewAssertableJSON(r.t, r.json, content) assert.Nil(r.t, err) for key, value := range data { @@ -386,7 +395,7 @@ func (r *TestResponseImpl) AssertFluentJson(callback func(json contractstesting. content, err := r.getContent() assert.Nil(r.t, err) - assertableJson, err := NewAssertableJSON(r.t, content) + assertableJson, err := NewAssertableJSON(r.t, r.json, content) assert.Nil(r.t, err) callback(assertableJson) diff --git a/testing/test_response_test.go b/testing/http/test_response_test.go similarity index 80% rename from testing/test_response_test.go rename to testing/http/test_response_test.go index ef74ce977..167d87cac 100644 --- a/testing/test_response_test.go +++ b/testing/http/test_response_test.go @@ -1,4 +1,4 @@ -package testing +package http import ( "html" @@ -17,19 +17,19 @@ import ( func TestAssertOk(t *testing.T) { res := createTestResponse(http.StatusOK) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertOk() } func TestAssertCreated(t *testing.T) { res := createTestResponse(http.StatusCreated) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertCreated() } func TestAssertAccepted(t *testing.T) { res := createTestResponse(http.StatusAccepted) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertAccepted() } @@ -37,127 +37,127 @@ func TestAssertNoContent(t *testing.T) { res := createTestResponse(http.StatusNoContent) res.Body = http.NoBody - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertNoContent() } func TestAssertMovedPermanently(t *testing.T) { res := createTestResponse(http.StatusMovedPermanently) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertMovedPermanently() } func TestAssertFound(t *testing.T) { res := createTestResponse(http.StatusFound) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertFound() } func TestAssertNotModified(t *testing.T) { res := createTestResponse(http.StatusNotModified) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertNotModified() } func TestAssertPartialContent(t *testing.T) { res := createTestResponse(http.StatusPartialContent) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertPartialContent() } func TestAssertTemporaryRedirect(t *testing.T) { res := createTestResponse(http.StatusTemporaryRedirect) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertTemporaryRedirect() } func TestAssertBadRequest(t *testing.T) { res := createTestResponse(http.StatusBadRequest) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertBadRequest() } func TestAssertUnauthorized(t *testing.T) { res := createTestResponse(http.StatusUnauthorized) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertUnauthorized() } func TestAssertPaymentRequired(t *testing.T) { res := createTestResponse(http.StatusPaymentRequired) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertPaymentRequired() } func TestAssertForbidden(t *testing.T) { res := createTestResponse(http.StatusForbidden) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertForbidden() } func TestAssertNotFound(t *testing.T) { res := createTestResponse(http.StatusNotFound) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertNotFound() } func TestAssertMethodNotAllowed(t *testing.T) { res := createTestResponse(http.StatusMethodNotAllowed) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertMethodNotAllowed() } func TestAssertNotAcceptable(t *testing.T) { res := createTestResponse(http.StatusNotAcceptable) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertNotAcceptable() } func TestAssertConflict(t *testing.T) { res := createTestResponse(http.StatusConflict) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertConflict() } func TestAssertRequestTimeout(t *testing.T) { res := createTestResponse(http.StatusRequestTimeout) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertRequestTimeout() } func TestAssertGone(t *testing.T) { res := createTestResponse(http.StatusGone) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertGone() } func TestAssertUnsupportedMediaType(t *testing.T) { res := createTestResponse(http.StatusUnsupportedMediaType) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertUnsupportedMediaType() } func TestAssertUnprocessableEntity(t *testing.T) { res := createTestResponse(http.StatusUnprocessableEntity) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertUnprocessableEntity() } func TestAssertTooManyRequests(t *testing.T) { res := createTestResponse(http.StatusTooManyRequests) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertTooManyRequests() } func TestAssertInternalServerError(t *testing.T) { res := createTestResponse(http.StatusInternalServerError) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertInternalServerError() } func TestAssertServiceUnavailable(t *testing.T) { res := createTestResponse(http.StatusServiceUnavailable) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertServiceUnavailable() } @@ -166,7 +166,7 @@ func TestAssertHeader(t *testing.T) { res := createTestResponse(http.StatusCreated) res.Header.Set(headerName, headerValue) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertHeader(headerName, headerValue).AssertCreated() } @@ -174,20 +174,20 @@ func TestAssertHeader(t *testing.T) { func TestAssertHeaderMissing(t *testing.T) { res := createTestResponse(http.StatusCreated) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertHeaderMissing("X-Custom-Header").AssertCreated() } func TestAssertSuccessful(t *testing.T) { res := createTestResponse(http.StatusPartialContent) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertSuccessful() } func TestServerError(t *testing.T) { res := createTestResponse(http.StatusInternalServerError) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertServerError() } @@ -200,7 +200,7 @@ func TestAssertCookie(t *testing.T) { HttpOnly: true, }).String()) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertCookie("session_id", "12345"). AssertCookieNotExpired("session_id"). @@ -215,7 +215,7 @@ func TestAssertCookieExpired(t *testing.T) { Expires: time.Now().Add(-24 * time.Hour), }).String()) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertCookie("session_id", "expired"). AssertCookieExpired("session_id") @@ -224,7 +224,7 @@ func TestAssertCookieExpired(t *testing.T) { func TestAssertCookieMissing(t *testing.T) { res := createTestResponse(http.StatusOK) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertCookieMissing("session_id") } @@ -233,7 +233,7 @@ func TestAssertSee(t *testing.T) { res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader("Hello, World! This is a test response.")) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertSee([]string{"Hello", "test"}) } @@ -242,7 +242,7 @@ func TestAssertSeeEscaped(t *testing.T) { escapedContent := html.EscapeString("
Hello, World!
") res.Body = io.NopCloser(strings.NewReader(escapedContent)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertSee([]string{"
Hello, World!
"}, true) } @@ -250,7 +250,7 @@ func TestAssertDontSee(t *testing.T) { res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader("This is a safe response.")) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertDontSee([]string{"error", "failure"}) } @@ -258,7 +258,7 @@ func TestAssertDontSeeEscaped(t *testing.T) { res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader("
Unauthorized access
")) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertDontSee([]string{"
Unauthorized access
"}, true) } @@ -266,60 +266,40 @@ func TestAssertSeeInOrder(t *testing.T) { res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader("Hello, this is a test for seeing values in order.")) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertSeeInOrder([]string{"Hello", "test", "values"}) } func TestAssertJson(t *testing.T) { - json = &testJson{} - defer func() { - json = nil - }() - res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader(`{"key1": "value1", "key2": 42}`)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertJson(map[string]any{"key1": "value1"}) } func TestAssertExactJson(t *testing.T) { - json = &testJson{} - defer func() { - json = nil - }() - res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader(`{"key1": "value1", "key2": 42}`)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertExactJson(map[string]any{"key1": "value1", "key2": float64(42)}) } func TestAssertJsonMissing(t *testing.T) { - json = &testJson{} - defer func() { - json = nil - }() - res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader(`{"key1": "value1", "key2": 42}`)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertJsonMissing(map[string]any{"key3": "value3"}) } func TestAssertFluentJson(t *testing.T) { - json = &testJson{} - defer func() { - json = nil - }() - sampleJson := `{"name": "krishan", "age": 22, "email": "krishan@example.com"}` res := createTestResponse(http.StatusOK) res.Body = io.NopCloser(strings.NewReader(sampleJson)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertFluentJson(func(json contractstesting.AssertableJSON) { json.Has("name").Where("name", "krishan") @@ -335,7 +315,7 @@ func TestAssertSeeInOrderWithEscape(t *testing.T) { escapedContent := html.EscapeString("Hello,
ordered
values") res.Body = io.NopCloser(strings.NewReader(escapedContent)) - r := NewTestResponse(t, res) + r := NewTestResponse(t, res, &testJson{}, nil) r.AssertSeeInOrder([]string{"Hello,", "
ordered
"}, true) } @@ -343,7 +323,6 @@ func TestSession_Success(t *testing.T) { mockSessionManager := mockssession.NewManager(t) mockDriver := mockssession.NewDriver(t) mockSession := mockssession.NewSession(t) - sessionFacade = mockSessionManager sessionData := map[string]any{ "user_id": 123, @@ -362,7 +341,9 @@ func TestSession_Success(t *testing.T) { response := createTestResponse(http.StatusOK) response.Header.Add("Set-Cookie", cookie.String()) - testResponse := &TestResponseImpl{} + testResponse := &TestResponseImpl{ + session: mockSessionManager, + } session, err := testResponse.Session() require.NoError(t, err) @@ -371,11 +352,12 @@ func TestSession_Success(t *testing.T) { func TestSession_DriverError(t *testing.T) { mockSessionManager := mockssession.NewManager(t) - sessionFacade = mockSessionManager mockSessionManager.On("Driver").Return(nil, errors.New("driver error")).Once() - testResponse := &TestResponseImpl{} + testResponse := &TestResponseImpl{ + session: mockSessionManager, + } _, err := testResponse.Session() require.EqualError(t, err, "driver error") @@ -384,21 +366,22 @@ func TestSession_DriverError(t *testing.T) { func TestSession_BuildSessionError(t *testing.T) { mockSessionManager := mockssession.NewManager(t) mockDriver := mockssession.NewDriver(t) - sessionFacade = mockSessionManager mockSessionManager.On("Driver").Return(mockDriver, nil).Once() mockSessionManager.On("BuildSession", mockDriver).Return(nil, errors.New("build session error")).Once() - testResponse := &TestResponseImpl{} + testResponse := &TestResponseImpl{ + session: mockSessionManager, + } _, err := testResponse.Session() require.EqualError(t, err, "build session error") } func TestSession_SessionFacadeNotSet(t *testing.T) { - sessionFacade = nil - - testResponse := &TestResponseImpl{} + testResponse := &TestResponseImpl{ + session: nil, + } _, err := testResponse.Session() require.ErrorIs(t, err, errors.SessionFacadeNotSet) diff --git a/testing/test_case.go b/testing/test_case.go index 1213f070f..4736f4ff4 100644 --- a/testing/test_case.go +++ b/testing/test_case.go @@ -7,13 +7,14 @@ import ( contractsseeder "github.com/goravel/framework/contracts/database/seeder" contractstesting "github.com/goravel/framework/contracts/testing" "github.com/goravel/framework/errors" + "github.com/goravel/framework/testing/http" ) type TestCase struct { } func (r *TestCase) Http(t *testing.T) contractstesting.TestRequest { - return NewTestRequest(t) + return http.NewTestRequest(t, json, routeFacade, sessionFacade) } func (r *TestCase) Seed(seeders ...contractsseeder.Seeder) {