@@ -10,6 +10,7 @@ import (
1010 "net/http"
1111 "net/http/httptest"
1212 "net/url"
13+ "strings"
1314 "testing"
1415
1516 "github.com/gofiber/fiber/v3"
@@ -200,6 +201,81 @@ func Test_HTTPMiddleware(t *testing.T) {
200201 require .Equal (t , "okay" , resp .Header .Get ("context_second_okay" ))
201202}
202203
204+ func Test_HTTPMiddlewareWithCookies (t * testing.T ) {
205+ const (
206+ cookieHeader = "Cookie"
207+ setCookieHeader = "Set-Cookie"
208+ cookieOneName = "cookieOne"
209+ cookieTwoName = "cookieTwo"
210+ cookieOneValue = "valueCookieOne"
211+ cookieTwoValue = "valueCookieTwo"
212+ )
213+ nethttpMW := func (next http.Handler ) http.Handler {
214+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
215+ if r .Method != http .MethodPost {
216+ w .WriteHeader (http .StatusMethodNotAllowed )
217+ return
218+ }
219+ next .ServeHTTP (w , r )
220+ })
221+ }
222+
223+ app := fiber .New ()
224+ app .Use (HTTPMiddleware (nethttpMW ))
225+ app .Post ("/" , func (c fiber.Ctx ) error {
226+ // RETURNING CURRENT COOKIES TO RESPONSE
227+ var cookies []string = strings .Split (c .Get (cookieHeader ), "; " )
228+ for _ , cookie := range cookies {
229+ c .Set (setCookieHeader , cookie )
230+ }
231+ return c .SendStatus (fiber .StatusOK )
232+ })
233+
234+ // Test case for POST request with cookies
235+ t .Run ("POST request with cookies" , func (t * testing.T ) {
236+ req , err := http .NewRequestWithContext (context .Background (), fiber .MethodPost , "/" , nil )
237+ require .NoError (t , err )
238+ req .AddCookie (& http.Cookie {Name : cookieOneName , Value : cookieOneValue })
239+ req .AddCookie (& http.Cookie {Name : cookieTwoName , Value : cookieTwoValue })
240+
241+ resp , err := app .Test (req )
242+ require .NoError (t , err )
243+ cookies := resp .Cookies ()
244+ require .Len (t , cookies , 2 )
245+ for _ , cookie := range cookies {
246+ switch cookie .Name {
247+ case cookieOneName :
248+ require .Equal (t , cookieOneValue , cookie .Value )
249+ case cookieTwoName :
250+ require .Equal (t , cookieTwoValue , cookie .Value )
251+ default :
252+ t .Error ("unexpected cookie key" )
253+ }
254+ }
255+ })
256+
257+ // New test case for GET request
258+ t .Run ("GET request" , func (t * testing.T ) {
259+ req , err := http .NewRequestWithContext (context .Background (), fiber .MethodGet , "/" , nil )
260+ require .NoError (t , err )
261+
262+ resp , err := app .Test (req )
263+ require .NoError (t , err )
264+ require .Equal (t , http .StatusMethodNotAllowed , resp .StatusCode )
265+ })
266+
267+ // New test case for request without cookies
268+ t .Run ("POST request without cookies" , func (t * testing.T ) {
269+ req , err := http .NewRequestWithContext (context .Background (), fiber .MethodPost , "/" , nil )
270+ require .NoError (t , err )
271+
272+ resp , err := app .Test (req )
273+ require .NoError (t , err )
274+ require .Equal (t , http .StatusOK , resp .StatusCode )
275+ require .Empty (t , resp .Cookies ())
276+ })
277+ }
278+
203279func Test_FiberHandler (t * testing.T ) {
204280 testFiberToHandlerFunc (t , false )
205281}
0 commit comments