diff --git a/contracts/documentation.yaml b/contracts/documentation.yaml index 4841715..6fe1f5e 100644 --- a/contracts/documentation.yaml +++ b/contracts/documentation.yaml @@ -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: @@ -115,7 +132,7 @@ paths: example: 404 description: type: string - example: Resource not found + example: Product not found name: type: string example: Not Found diff --git a/contracts/products.spec b/contracts/products.spec index 98f4858..dee7c37 100644 --- a/contracts/products.spec +++ b/contracts/products.spec @@ -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 @@ -16,6 +23,7 @@ Feature: Products API Examples: | id | | 1 | + | 4 | Scenario Outline: Products List Success When GET /v1/products diff --git a/server.py b/server.py index 5aa9518..1495a48 100644 --- a/server.py +++ b/server.py @@ -1,4 +1,5 @@ from flask import abort, json, Flask +from werkzeug.exceptions import HTTPException app = Flask(__name__) @@ -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({ @@ -32,8 +33,16 @@ def index(): return response -@app.route('/v1/products/', methods=['GET']) +@app.route('/v1/products/', 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: @@ -44,4 +53,4 @@ def get(id): ) return response - abort(404, description="Resource not found") + abort(404, description='Product not found')