Skip to content

Commit

Permalink
Add test for 400 response
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Feb 4, 2023
1 parent 22e80b4 commit db41ed4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
19 changes: 18 additions & 1 deletion contracts/documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Product'
'400':
description: Bad Request
content:
application/json:
schema:
type: object
properties:
code:
type: integer
format: int32
example: 400
description:
type: string
example: Invalid product ID data type
name:
type: string
example: Bad Request
'404':
description: Not Found
content:
Expand All @@ -115,7 +132,7 @@ paths:
example: 404
description:
type: string
example: Resource not found
example: Product not found
name:
type: string
example: Not Found
8 changes: 8 additions & 0 deletions contracts/products.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Feature: Products API
Background:
Given openapi ./documentation.yaml

Scenario Outline: Get Product Success
When GET /v1/products/(id:number)
Then status 400
Examples:
| id |
| 1.1 |

Scenario Outline: Get Product Not Found Error
When GET /v1/products/(id:number)
Then status 404
Expand All @@ -16,6 +23,7 @@ Feature: Products API
Examples:
| id |
| 1 |
| 4 |

Scenario Outline: Products List Success
When GET /v1/products
Expand Down
15 changes: 12 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import abort, json, Flask
from werkzeug.exceptions import HTTPException


app = Flask(__name__)
Expand All @@ -9,7 +10,7 @@ def get_products_list():
return json.loads(f.read())


@app.errorhandler(404)
@app.errorhandler(HTTPException)
def resource_not_found(e):
response = e.get_response()
response.data = json.dumps({
Expand All @@ -32,8 +33,16 @@ def index():
return response


@app.route('/v1/products/<int:id>', methods=['GET'])
@app.route('/v1/products/<id>', methods=['GET'])
def get(id):
# This check is made on purpose to simulate 400 error
try:
if str(int(id)) != str(id):
raise ValueError()
id = int(id)
except ValueError:
abort(400, description='Invalid product ID data type')

data = get_products_list()
for product in data:
if product['id'] == id:
Expand All @@ -44,4 +53,4 @@ def get(id):
)
return response

abort(404, description="Resource not found")
abort(404, description='Product not found')

0 comments on commit db41ed4

Please sign in to comment.