Skip to content

Commit

Permalink
Fix the regex for path segment capture (#44)
Browse files Browse the repository at this point in the history
* Fix the regex for path segment capture

* Add a test related to path segment capture conflict

* Fix a flake8 error
  • Loading branch information
M. Mert Yıldıran authored Jan 22, 2021
1 parent bfc113b commit f23b074
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mockintosh/recognizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def render_segment(self, text):
match = re.search(r'{{(.*)}}', text)
if match is not None:
name = match.group(1).strip()
compiled = '.*'
compiled = '[^/]+'
var = name
else:
compiled = text
Expand Down
47 changes: 47 additions & 0 deletions tests/configs/json/hbs/path/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,53 @@
},
"body": "@templates/5.json.hbs"
}
},
{
"method": "delete",
"path": "/carts/{{customerId}}",
"response": {
"status": 202
}
},
{
"method": "post",
"path": "/carts/{{customerId}}/items",
"response": {
"body": "{\n \"id\": \"L8VEqJRB4R\",\n \"itemId\": \"{{request.body}}\",\n \"quantity\": 1,\n \"unitPrice\": 12.0\n}",
"headers": {
"Content-Type": "application/json"
},
"status": 201
}
},
{
"path": "/carts/{{customerId}}/merge",
"response": {
"status": 202
}
},
{
"path": "/carts/{{customerId}}/items",
"response": {
"body": "[]",
"headers": {
"Content-Type": "application/json"
}
}
},
{
"method": "patch",
"path": "/carts/{{customerId}}/items",
"response": {
"status": 202
}
},
{
"method": "delete",
"path": "/carts/{{customerId}}/items/{{itemId}}",
"response": {
"status": 202
}
}
]
}
Expand Down
47 changes: 47 additions & 0 deletions tests/configs/json/j2/path/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,53 @@
},
"body": "@templates/5.json.j2"
}
},
{
"method": "delete",
"path": "/carts/{{customerId}}",
"response": {
"status": 202
}
},
{
"method": "post",
"path": "/carts/{{customerId}}/items",
"response": {
"body": "{\n \"id\": \"L8VEqJRB4R\",\n \"itemId\": \"{{request.body}}\",\n \"quantity\": 1,\n \"unitPrice\": 12.0\n}",
"headers": {
"Content-Type": "application/json"
},
"status": 201
}
},
{
"path": "/carts/{{customerId}}/merge",
"response": {
"status": 202
}
},
{
"path": "/carts/{{customerId}}/items",
"response": {
"body": "[]",
"headers": {
"Content-Type": "application/json"
}
}
},
{
"method": "patch",
"path": "/carts/{{customerId}}/items",
"response": {
"status": 202
}
},
{
"method": "delete",
"path": "/carts/{{customerId}}/items/{{itemId}}",
"response": {
"status": 202
}
}
]
}
Expand Down
33 changes: 33 additions & 0 deletions tests/configs/yaml/hbs/path/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,36 @@ services:
headers:
Content-Type: "application/json; charset=UTF-8"
body: "@templates/5.json.hbs"
- method: delete
path: /carts/{{customerId}}
response:
status: 202
- method: post
path: /carts/{{customerId}}/items
response:
body: |-
{
"id": "L8VEqJRB4R",
"itemId": "{{request.body}}",
"quantity": 1,
"unitPrice": 12.0
}
headers:
Content-Type: application/json
status: 201
- path: /carts/{{customerId}}/merge
response:
status: 202
- path: /carts/{{customerId}}/items
response:
body: '[]'
headers:
Content-Type: application/json
- method: patch
path: /carts/{{customerId}}/items
response:
status: 202
- method: delete
path: /carts/{{customerId}}/items/{{itemId}}
response:
status: 202
33 changes: 33 additions & 0 deletions tests/configs/yaml/j2/path/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,36 @@ services:
headers:
Content-Type: "application/json; charset=UTF-8"
body: "@templates/5.json.j2"
- method: delete
path: /carts/{{customerId}}
response:
status: 202
- method: post
path: /carts/{{customerId}}/items
response:
body: |-
{
"id": "L8VEqJRB4R",
"itemId": "{{request.body}}",
"quantity": 1,
"unitPrice": 12.0
}
headers:
Content-Type: application/json
status: 201
- path: /carts/{{customerId}}/merge
response:
status: 202
- path: /carts/{{customerId}}/items
response:
body: '[]'
headers:
Content-Type: application/json
- method: patch
path: /carts/{{customerId}}/items
response:
status: 202
- method: delete
path: /carts/{{customerId}}/items/{{itemId}}
response:
status: 202
27 changes: 27 additions & 0 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,33 @@ def test_multiple_parameters_and_regex_capture_groups(self, config):
assert data['var4'] == param4
assert data['var5'] == param5

def test_path_segment_capture_conflict(self, config):
param1 = str(int(time.time()))
param2 = str(int(time.time()))
resp = requests.delete(SRV_8001 + '/carts/%s' % param1)
assert 202 == resp.status_code

resp = requests.post(SRV_8001 + '/carts/%s/items' % param1)
assert 201 == resp.status_code
assert resp.headers['Content-Type'] == 'application/json'
data = resp.json()
assert data['id'] == 'L8VEqJRB4R'

resp = requests.get(SRV_8001 + '/carts/%s/merge' % param1)
assert 202 == resp.status_code

resp = requests.get(SRV_8001 + '/carts/%s/items' % param1)
assert 200 == resp.status_code
assert resp.headers['Content-Type'] == 'application/json'
data = resp.json()
assert isinstance(data, list) and not data

resp = requests.patch(SRV_8001 + '/carts/%s/items' % param1)
assert 202 == resp.status_code

resp = requests.delete(SRV_8001 + '/carts/%s/items/%s' % (param1, param2))
assert 202 == resp.status_code


@pytest.mark.parametrize(('config'), [
'configs/json/hbs/query_string/config.json',
Expand Down

0 comments on commit f23b074

Please sign in to comment.