/
collect_endpoint.feature
160 lines (157 loc) · 6.73 KB
/
collect_endpoint.feature
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Feature: Collect endpoints when running tests
In order to keep up to date my API documentation
As a jadd user
I should collect endpoints result from tests
Scenario: Run behat tests
Given a file named "behat.yml" with:
"""
default:
extensions:
Rezzza\MocoBehatExtension\MocoExtension:
json_file: features/fixtures.yml
hostname: 127.0.0.1
port: 9999
Rezzza\RestApiBehatExtension\Extension:
rest:
base_url: http://127.0.0.1:9999
suites:
default:
contexts:
- FeatureContext
- Rezzza\RestApiBehatExtension\RestApiContext
- Rezzza\MocoBehatExtension\MocoContext
"""
And a file named "features/bootstrap/FeatureContext.php" with:
"""
<?php
use Behat\Behat\Context\Context;
use Rezzza\MocoBehatExtension\MocoWriter;
use Behat\Gherkin\Node\PyStringNode;
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser;
use Rezzza\Jadd\Domain\EndpointCollector;
class FeatureContext implements Context
{
private $mocoWriter;
public function __construct(MocoWriter $mocoWriter, RestApiBrowser $restApiBrowser)
{
$this->mocoWriter = $mocoWriter;
$restApiBrowser->useHttpClient(
new \Http\Client\Common\PluginClient(
new \Http\Client\Curl\Client,
[new \Rezzza\Jadd\Infra\Http\CollectEndpointPlugin]
)
);
EndpointCollector::reset();
}
/**
* @Given I mock the following endpoint:
*/
public function mockHttpCall(PyStringNode $endpoint)
{
$json = json_decode((string) $endpoint, true);
$this->mocoWriter->mockHttpCall(
$json['request'],
$json['response']
);
$this->mocoWriter->writeForMoco();
}
}
"""
And a file named "features/call_moco.feature" with:
"""
Feature: Call Moco
In order to mock external call
As a feature runner
I need to call moco
@moco
Scenario: Call moco
Given I mock the following endpoint:
'''
{
"request": {
"uri": "/hotels/wrong",
"method": "GET",
"headers": {
"content-type" : "application/json"
}
},
"response": {
"status" : 502,
"text": "Bad Gateway"
}
}
'''
And I mock the following endpoint:
'''
{
"request": {
"uri": "/hotels",
"method": "POST",
"headers": {
"content-type" : "application/json"
}
},
"response": {
"status" : 201,
"headers": {
"Content-Type": "application/json",
"Location": "/hotels/123"
}
}
}
'''
And I mock the following endpoint:
'''
{
"request": {
"uri": "/hotels",
"method": "POST",
"headers": {
"content-type" : "text/html"
}
},
"response": {
"status" : 400,
"headers": {
"Content-Type": "application/json"
},
"json": {"errors": ["invalid data"]}
}
}
'''
And I mock the following endpoint:
'''
{
"request": {
"uri": "/hotels/123",
"method": "GET"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"json": {"name": "hotel blue"}
}
}
'''
When I set "Content-Type" header equal to "application/json"
And I send a GET request to "/hotels/wrong"
And I send a POST request to "/hotels" with body:
'''
{"name":"hotel blue"}
'''
And I set "Content-Type" header equal to "text/html"
And I send a POST request to "/hotels"
When I set "Content-Type" header equal to "application/json"
And I send a GET request to "/hotels/123"
Then print response
"""
And I start moco
When I run behat "features/call_moco.feature"
Then the tests should have collected the following endpoints:
"""
POST,/hotels,application/json,[],"{""name"":""hotel blue""}",201,application/json,"{""Location"":[""\/hotels\/123""]}",
POST,/hotels,text/html,[],,400,application/json,[],"{""errors"":[""invalid data""]}"
GET,/hotels/123,application/json,[],,200,application/json,[],"{""name"":""hotel blue""}"
"""