Skip to content

Commit

Permalink
Support repeated requests (#6)
Browse files Browse the repository at this point in the history
* Support repeated requests

* Update README.md
  • Loading branch information
nhatthm authored Dec 14, 2021
1 parent 5c4ef66 commit b63d793
Show file tree
Hide file tree
Showing 8 changed files with 539 additions and 46 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,26 @@ func TestIntegration(t *testing.T) {

Mock a new request with (one of) these patterns

- `^"([^"]*)" receives a (?:gRPC|GRPC|grpc) request "([^"]*)"$`
- `^"([^"]*)" receives a (?:gRPC|GRPC|grpc) request "([^"]*)" with payload:$`
- `^"([^"]*)" receives a (?:gRPC|GRPC|grpc) request "([^"]*)" with payload from file "([^"]+)"$`
- `^"([^"]*)" receives a (?:gRPC|GRPC|grpc) request "([^"]*)" with payload from file:$`
- `^"([^"]*)" receives [a1] (?:gRPC|GRPC|grpc) request "([^"]*)"$`
- `^"([^"]*)" receives [a1] (?:gRPC|GRPC|grpc) request "([^"]*)" with payload:$`
- `^"([^"]*)" receives [a1] (?:gRPC|GRPC|grpc) request "([^"]*)" with payload from file "([^"]+)"$`
- `^"([^"]*)" receives [a1] (?:gRPC|GRPC|grpc) request "([^"]*)" with payload from file:$`

Optionally, you can:
Or, if the service receives multiple requests with the same condition, you could use

- `^"([^"]*)" receives ([0-9]+) (?:gRPC|GRPC|grpc) requests "([^"]*)"$`
- `^"([^"]*)" receives ([0-9]+) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload:$`
- `^"([^"]*)" receives ([0-9]+) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload from file "([^"]+)"$`
- `^"([^"]*)" receives ([0-9]+) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload from file:$`

Or, if you don't know how many times it's going to be, use

- `^"([^"]*)" receives (?:some|many|several) (?:gRPC|GRPC|grpc) requests "([^"]*)"$`
- `^"([^"]*)" receives (?:some|many|several) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload:$`
- `^"([^"]*)" receives (?:some|many|several) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload from file "([^"]+)"$`
- `^"([^"]*)" receives (?:some|many|several) (?:gRPC|GRPC|grpc) requests "([^"]*)" with payload from file:$`

And, Optionally, you can:

- Add a header to the request with <br/>
`^The (?:gRPC|GRPC|grpc) request has(?: a)? header "([^"]*): ([^"]*)"$`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Error when expectations were not met
Feature: Expect a request but receive nothing

Scenario: there is one expectation
Scenario: there is one request
Given "item-service" receives a grpc request "/grpctest.ItemService/GetItem" with payload:
"""
{
Expand All @@ -10,7 +10,7 @@ Feature: Error when expectations were not met
And the grpc service responds with payload:
"""
{
"id": 42
"id": 42,
"name": "Item #42"
}
"""
16 changes: 16 additions & 0 deletions features/server/ErrorExpectSeveralRequestsButNoReceive.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Expect several requests but receive nothing

Scenario: expect several requests
Given "item-service" receives several grpc requests "/grpctest.ItemService/GetItem" with payload:
"""
{
"id": 42
}
"""
And the grpc service responds with payload:
"""
{
"id": 42,
"name": "Item #42"
}
"""
31 changes: 31 additions & 0 deletions features/server/ErrorExpectTwoRequestsButReceiveOne.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: Expect 2 requests but receive only 1

Scenario: receive only 1 request
Given "item-service" receives 2 grpc requests "/grpctest.ItemService/GetItem" with payload:
"""
{
"id": 42
}
"""
And the grpc service responds with payload:
"""
{
"id": 42,
"name": "Item #42"
}
"""

When I request a grpc method "/grpctest.ItemService/GetItem" with payload:
"""
{
"id": 42
}
"""

Then I should have a grpc response with payload:
"""
{
"id": 42,
"name": "Item #42"
}
"""
282 changes: 282 additions & 0 deletions features/server/Success.feature
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,285 @@ Feature: Test all supported types with all statements
| GetItem | resources/fixtures/request-get-item.json | resources/fixtures/response-get-item.json |
| ListItems | resources/fixtures/request-list-items.json | resources/fixtures/response-list-items.json |
| CreateItems | resources/fixtures/request-create-items.json | resources/fixtures/response-create-items.json |

Scenario Outline: Request 2 times without payload
Given "item-service" receives 2 grpc requests "/grpctest.ItemService/<method>"
And the grpc service responds with code "InvalidArgument"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

Examples:
| method | request |
| GetItem | {"id":42} |
| ListItems | {} |
| CreateItems | [{"id":42}] |

Scenario Outline: Request 2 times with payload
Given "item-service" receives 2 grpc requests "/grpctest.ItemService/<method>" with payload:
"""
<expect>
"""
And the grpc service responds with code "InvalidArgument"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

Examples:
| method | expect | request |
| GetItem | {"id": "<ignore-diff>"} | {"id":42} |
| ListItems | {} | {} |
| CreateItems | [{"id": "<ignore-diff>"}] | [{"id":42}] |

Scenario Outline: Request 2 times with payload from file
Given "item-service" receives 2 grpc requests "/grpctest.ItemService/<method>" with payload from file "<request_file>"
And the grpc service responds with payload from file "<response_file>"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

Examples:
| method | request_file | response_file |
| GetItem | resources/fixtures/request-get-item.json | resources/fixtures/response-get-item.json |
| ListItems | resources/fixtures/request-list-items.json | resources/fixtures/response-list-items.json |
| CreateItems | resources/fixtures/request-create-items.json | resources/fixtures/response-create-items.json |

Scenario Outline: Request 2 times with payload from file in doc string
Given "item-service" receives 2 grpc requests "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""
And the grpc service responds with payload from file "<response_file>"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

Examples:
| method | request_file | response_file |
| GetItem | resources/fixtures/request-get-item.json | resources/fixtures/response-get-item.json |
| ListItems | resources/fixtures/request-list-items.json | resources/fixtures/response-list-items.json |
| CreateItems | resources/fixtures/request-create-items.json | resources/fixtures/response-create-items.json |

Scenario Outline: Request several times without payload
Given "item-service" receives some grpc requests "/grpctest.ItemService/<method>"
And the grpc service responds with code "InvalidArgument"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 3rd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

Examples:
| method | request |
| GetItem | {"id":42} |
| ListItems | {} |
| CreateItems | [{"id":42}] |

Scenario Outline: Request several times with payload
Given "item-service" receives some grpc requests "/grpctest.ItemService/<method>" with payload:
"""
<expect>
"""
And the grpc service responds with code "InvalidArgument"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

# 3rd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload:
"""
<request>
"""

Then I should have a grpc response with code "InvalidArgument"

Examples:
| method | expect | request |
| GetItem | {"id": "<ignore-diff>"} | {"id":42} |
| ListItems | {} | {} |
| CreateItems | [{"id": "<ignore-diff>"}] | [{"id":42}] |

Scenario Outline: Request several times with payload from file
Given "item-service" receives some grpc requests "/grpctest.ItemService/<method>" with payload from file "<request_file>"
And the grpc service responds with payload from file "<response_file>"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 3rd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

Examples:
| method | request_file | response_file |
| GetItem | resources/fixtures/request-get-item.json | resources/fixtures/response-get-item.json |
| ListItems | resources/fixtures/request-list-items.json | resources/fixtures/response-list-items.json |
| CreateItems | resources/fixtures/request-create-items.json | resources/fixtures/response-create-items.json |

Scenario Outline: Request several times with payload from file in doc string
Given "item-service" receives some grpc requests "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""
And the grpc service responds with payload from file "<response_file>"

# 1st request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 2nd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

# 3rd request.
When I request a grpc method "/grpctest.ItemService/<method>" with payload from file:
"""
<request_file>
"""

Then I should have a grpc response with payload from file:
"""
<response_file>
"""

Examples:
| method | request_file | response_file |
| GetItem | resources/fixtures/request-get-item.json | resources/fixtures/response-get-item.json |
| ListItems | resources/fixtures/request-list-items.json | resources/fixtures/response-list-items.json |
| CreateItems | resources/fixtures/request-create-items.json | resources/fixtures/response-create-items.json |
Loading

0 comments on commit b63d793

Please sign in to comment.