Skip to content

Commit 340d357

Browse files
authored
Merge pull request #169 from mirumee/missing_content_type
Missing Content-Type
2 parents 587e492 + 18764fc commit 340d357

14 files changed

Lines changed: 84 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## UNRELEASED
4+
5+
- Fixed `AsyncBaseClient` and `BaseClient` to send `Content-Type` header with requests.
6+
7+
38
## 0.7.0 (2023-06-01)
49

510
- Added support for subscriptions as async generators.

ariadne_codegen/client_generators/dependencies/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

ariadne_codegen/client_generators/dependencies/base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def execute(
4545
if variables:
4646
payload["variables"] = self._convert_dict_to_json_serializable(variables)
4747
content = json.dumps(payload, default=pydantic_encoder)
48-
return self.http_client.post(url=self.url, content=content)
48+
return self.http_client.post(
49+
url=self.url, content=content, headers={"Content-Type": "application/json"}
50+
)
4951

5052
def get_data(self, response: httpx.Response) -> dict[str, Any]:
5153
if not response.is_success:

tests/client_generators/dependencies/test_async_base_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,31 @@ async def test_execute_sends_payload_with_serialized_datetime_without_exception(
185185
assert content["variables"]["arg"] == arg_value.isoformat()
186186

187187

188+
@pytest.mark.asyncio
189+
async def test_execute_sends_request_with_correct_content_type(httpx_mock):
190+
httpx_mock.add_response()
191+
client = AsyncBaseClient(url="http://base_url")
192+
193+
await client.execute("query Abc { abc }", {})
194+
195+
request = httpx_mock.get_request()
196+
assert request.headers["Content-Type"] == "application/json"
197+
198+
199+
@pytest.mark.asyncio
200+
async def test_execute_sends_request_with_extra_headers_and_correct_content_type(
201+
httpx_mock,
202+
):
203+
httpx_mock.add_response()
204+
client = AsyncBaseClient(url="http://base_url", headers={"h_key": "h_value"})
205+
206+
await client.execute("query Abc { abc }", {})
207+
208+
request = httpx_mock.get_request()
209+
assert request.headers["h_key"] == "h_value"
210+
assert request.headers["Content-Type"] == "application/json"
211+
212+
188213
@pytest.mark.parametrize(
189214
"status_code, response_content",
190215
[

tests/client_generators/dependencies/test_base_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,27 @@ def test_execute_sends_payload_with_serialized_datetime_without_exception(httpx_
172172
assert content["variables"]["arg"] == arg_value.isoformat()
173173

174174

175+
def test_execute_sends_request_with_correct_content_type(httpx_mock):
176+
httpx_mock.add_response()
177+
client = BaseClient(url="http://base_url")
178+
179+
client.execute("query Abc { abc }", {})
180+
181+
request = httpx_mock.get_request()
182+
assert request.headers["Content-Type"] == "application/json"
183+
184+
185+
def test_execute_sends_request_with_extra_headers_and_correct_content_type(httpx_mock):
186+
httpx_mock.add_response()
187+
client = BaseClient(url="http://base_url", headers={"h_key": "h_value"})
188+
189+
client.execute("query Abc { abc }", {})
190+
191+
request = httpx_mock.get_request()
192+
assert request.headers["h_key"] == "h_value"
193+
assert request.headers["Content-Type"] == "application/json"
194+
195+
175196
@pytest.mark.parametrize(
176197
"status_code, response_content",
177198
[

tests/main/clients/custom_config_file/expected_client/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

tests/main/clients/custom_files_names/expected_client/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

tests/main/clients/custom_scalars/expected_client/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

tests/main/clients/example/expected_client/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

tests/main/clients/extended_models/expected_client/async_base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ async def execute(
8888
if variables:
8989
payload["variables"] = self._convert_dict_to_json_serializable(variables)
9090
content = json.dumps(payload, default=pydantic_encoder)
91-
return await self.http_client.post(url=self.url, content=content)
91+
return await self.http_client.post(
92+
url=self.url, content=content, headers={"Content-Type": "application/json"}
93+
)
9294

9395
def get_data(self, response: httpx.Response) -> Dict[str, Any]:
9496
if not response.is_success:

0 commit comments

Comments
 (0)