Skip to content

Commit

Permalink
fix: python
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Sep 26, 2024
1 parent 7a6d16e commit bd53120
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mock-server/app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
]);
});

App::get('/v1/mock/tests/general/multipart-echo')
App::post('/v1/mock/tests/general/multipart-echo')
->desc('Multipart echo')
->groups(['mock'])
->label('scope', 'public')
Expand All @@ -422,7 +422,7 @@
->label('sdk.response.type', Response::CONTENT_TYPE_MULTIPART)
->label('sdk.response.model', Response::MODEL_MULTIPART)
->label('sdk.mock', true)
->param('body', '', new Text(100), 'Sample string param', false, [], true)
->param('body', '', new File(), 'Sample file param', false, [], true)
->inject('response')
->action(function (string $body, Response $response) {

Expand Down
7 changes: 5 additions & 2 deletions templates/python/package/payload.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ class Payload:

return self._data[offset:offset + length]

def to_string(self, encoding='utf-8') -> str:
def to_string(self, encoding="utf-8") -> str:
return self.to_binary().decode(encoding)

def __str__(self) -> str:
return self.to_string()

def to_json(self) -> Dict[str, Any]:
return json.loads(self.to_string())

Expand All @@ -60,4 +63,4 @@ class Payload:

@classmethod
def from_json(cls, data: Dict[str, Any]) -> 'Payload':
return cls(data=json.dumps(data))
return cls.from_string(json.dumps(data))
8 changes: 7 additions & 1 deletion tests/languages/python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@
print(response['x']) # should be "abc"
print(md5(response['responseBody'].to_binary()).hexdigest()) # should be d80e7e6999a3eb2ae0d631a96fe135a4

response = general.multipart_json()
response = general.multipart_echo(Payload.from_string("Hello, World!"))
print(response['responseBody'].to_string())

response = general.multipart_echo(Payload.from_json({"key": "myStringValue"}))
print(response['responseBody'].to_json()['key'])

response = general.multipart_echo(Payload.from_file('./tests/resources/file.png'))
response['responseBody'].to_file('./tests/resources/file_copy.png')
print(md5(open('./tests/resources/file.png', 'rb').read()).hexdigest())

# Query helper tests
print(Query.equal("released", [True]))
print(Query.equal("title", ["Spiderman", "Dr. Strange"]))
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@
}
},
"\/mock\/tests\/general\/multipart-echo": {
"get": {
"post": {
"summary": "MultipartEcho",
"operationId": "generalMultipartEcho",
"consumes": [
Expand Down

0 comments on commit bd53120

Please sign in to comment.