1+ import json
12import pytest
23from unittest .mock import MagicMock
34from wc_client .request import WCRequest
@@ -8,6 +9,12 @@ def __init__(self, content, status_code=200):
89 self .content = content
910 self .status_code = status_code
1011
12+ def json (self ):
13+ # Decode bytes to string
14+ json_str = self .content .decode ("utf-8" )
15+ # Convert string to dict
16+ return json .loads (json_str )
17+
1118
1219@pytest .fixture
1320def wc_request ():
@@ -74,3 +81,49 @@ def test_make_request(wc_request):
7481 )
7582 assert response .status_code == 200
7683 assert response .content == b'{"key": "value"}'
84+
85+
86+ def test_make_request_paginated (wc_request ):
87+ # Mocking httpx.Client.get for the paginated case
88+ client = MagicMock ()
89+ client .__enter__ .return_value = client
90+ client .__exit__ .return_value = False
91+ client .get .side_effect = [
92+ MockResponse (b'[{"foo": "bar"}, {"fizz": "buzz"}]' , 200 ),
93+ MockResponse (b"[]" , 200 ),
94+ ]
95+ wc_request .client = client
96+
97+ data = wc_request .paginated (
98+ query_params = {"param" : "value" }, headers = {"Authorization" : "foo-bar" }
99+ )
100+
101+ assert client .get .call_count == 2
102+
103+ calls = client .get .call_args_list
104+ first_call = calls [0 ].kwargs
105+ second_call = calls [1 ].kwargs
106+
107+ assert first_call ["url" ] == "https://example.com/consumer/orders/1"
108+ assert first_call ["headers" ] == {
109+ "Accept" : "application/json" ,
110+ "Authorization" : "foo-bar" ,
111+ }
112+ assert first_call ["params" ] == {
113+ "param" : "value" ,
114+ "per_page" : WCRequest .DEFAULT_PAGE_LIMIT ,
115+ "page" : 1 ,
116+ }
117+
118+ assert second_call ["url" ] == "https://example.com/consumer/orders/1"
119+ assert second_call ["headers" ] == {
120+ "Accept" : "application/json" ,
121+ "Authorization" : "foo-bar" ,
122+ }
123+ assert second_call ["params" ] == {
124+ "param" : "value" ,
125+ "per_page" : WCRequest .DEFAULT_PAGE_LIMIT ,
126+ "page" : 2 ,
127+ }
128+
129+ assert data == [{"foo" : "bar" }, {"fizz" : "buzz" }]
0 commit comments