Repository navigation
Expand file tree
/
Copy pathValidateResponseTest.php
More file actions
114 lines (90 loc) · 4.38 KB
/
Copy pathValidateResponseTest.php
File metadata and controls
114 lines (90 loc) · 4.38 KB
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
<?php
declare(strict_types=1);
namespace OpenAPIValidationTests\PSR7;
use GuzzleHttp\Psr7\Response;
use OpenAPIValidation\PSR7\Exception\Validation\InvalidBody;
use OpenAPIValidation\PSR7\Exception\Validation\InvalidHeaders;
use OpenAPIValidation\PSR7\OperationAddress;
use OpenAPIValidation\PSR7\ValidatorBuilder;
use function GuzzleHttp\Psr7\stream_for;
use function json_encode;
final class ValidateResponseTest extends BaseValidatorTest
{
public function testItValidatesMessageGreen() : void
{
$response = $this->makeGoodResponse('/path1', 'get');
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate(new OperationAddress('/path1', 'get'), $response);
$this->addToAssertionCount(1);
}
public function testItValidatesMessageWithReferencesGreen() : void
{
$body = [
'name' => 'good name',
'age' => 100,
];
$response = (new Response())
->withHeader('Content-Type', 'application/json')
->withBody(stream_for(json_encode($body)));
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate(new OperationAddress('/ref', 'post'), $response);
$this->addToAssertionCount(1);
}
public function testItValidatesBinaryResponseGreen() : void
{
$response = $this->makeGoodResponse('/path1', 'get')
->withHeader('Content-Type', 'image/jpeg')
->withBody(stream_for(__DIR__ . '/../stubs/image.jpg'));
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate(new OperationAddress('/path1', 'get'), $response);
$this->addToAssertionCount(1);
}
public function testItValidatesMessageWrongBodyValueRed() : void
{
$addr = new OperationAddress('/path1', 'get');
$body = [];
$response = $this->makeGoodResponse('/path1', 'get')->withBody(stream_for(json_encode($body)));
$this->expectException(InvalidBody::class);
$this->expectExceptionMessage(
'Body does not match schema for content-type "application/json" for Response [get /path1 200]'
);
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate($addr, $response);
}
public function testItValidatesMessageWrongHeaderValueRed() : void
{
$addr = new OperationAddress('/path1', 'get');
$response = $this->makeGoodResponse('/path1', 'get')->withHeader('Header-B', 'wrong value');
$this->expectException(InvalidHeaders::class);
$this->expectExceptionMessage(
'Value "wrong value" for header "Header-B" is invalid for Response [get /path1 200]'
);
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate($addr, $response);
}
public function testItValidatesMessageMissesHeaderRed() : void
{
$addr = new OperationAddress('/path1', 'get');
$response = $this->makeGoodResponse('/path1', 'get')->withoutHeader('Header-B');
$this->expectException(InvalidHeaders::class);
$this->expectExceptionMessage('Missing required header "Header-B" for Response [get /path1 200]');
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate($addr, $response);
}
public function testItValidatesEmptyBodyResponseGreen() : void
{
$addr = new OperationAddress('/empty', 'post');
$response = new Response(204); // no body response
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate($addr, $response);
$this->addToAssertionCount(1);
}
public function testItValidatesDefaultBodyResponseGreen() : void
{
$addr = new OperationAddress('/empty', 'patch'); // "patch" contains "default" response definition
$response = new Response(404); // dummy any status code
$validator = (new ValidatorBuilder())->fromYamlFile($this->apiSpecFile)->getResponseValidator();
$validator->validate($addr, $response);
$this->addToAssertionCount(1);
}
}