Skip to content

Commit

Permalink
[doc] add a document to test invalid input
Browse files Browse the repository at this point in the history
- #88
- also update some document
  • Loading branch information
mission-liao committed Oct 1, 2016
1 parent 5224208 commit 84d2ea5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Read the [Document](http://pyswagger.readthedocs.org/en/latest/), or just go thr
- [Exntending Primitive Factory for user-defined primitives](docs/md/tutorial/extend_prim.md)
- [Rendering Random Requests for BlackBox Testing](docs/md/tutorial/render.md)
- [Operation MIME Support](docs/md/tutorial/mime.md)
- [Test with Invalid Input](docs/md/tutorial/invalid.md)

---------

Expand Down
30 changes: 30 additions & 0 deletions docs/md/tutorial/invalid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### Test With Invalid Input

All you provided for **pyswagger.spec.v2_0.objects.Operation** would be inspected / validated based on the loaded Swagger (OpenAPI) spec. Therefore, when using pyswagger as an API client, this behavior is relevant, but it's just not required when you want to provide some invalid input for your own server.

Can those validation be skipped? No, but pyswagger allows you to patch them after "prepared". Taking an example from this [issue](https://github.com/mission-liao/pyswagger/issues/88)

```python
from pyswagger import App
from pyswagger.contrib.client.requests import Client

app = App._create_('http://petstore.swagger.io/v2/swagger.json')
client = Client()

pet_tom = dict(
id=123,
name='Tom',
photoUrls='https://github.com/',
status='available')

req, resp = app.op['addPet'](body=pet_tom)

# patching the "body" parameter named "body", ... which seems cumbersome
req._p['body']['body']['id'] = 'not_valid'

resp = client.request((req, resp,))
print resp.status # the code I tried is 500, not 405
```

You can access those "prepared" parameters via **pyswagger.io.Request._p** property, it's a *dict*, which is arranging the parameters you provided by "type" and "name" defined in [OpenAPI spec](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object). To be noted: when the name of "type" changed in later version of OpenAPI, **the key of _p would also be changed**.

8 changes: 4 additions & 4 deletions pyswagger/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ def prepare(self, scheme='http', handle_files=True, encoding='utf-8'):

@property
def url(self):
""" url of this request
""" url of this request, only valid after 'prepare'
:type: str
"""
return self.__url

@property
def path(self):
""" path of this request
""" path of this request, only valid after 'prepare'
:type: str
"""
Expand Down Expand Up @@ -251,15 +251,15 @@ def method(self):

@property
def header(self):
""" header of this request
""" header of this request, only valid after 'prepare'
:type: dict
"""
return self.__header

@property
def data(self):
""" data carried by this request
""" data carried by this request, only valid after 'prepare'
:type: byte
"""
Expand Down

0 comments on commit 84d2ea5

Please sign in to comment.