Skip to content

Commit 267e0d2

Browse files
franzliedkelstrojny
authored andcommitted
Implement and test header matchers (#44)
1 parent 0e10f20 commit 267e0d2

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/Expectation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public function queryParamsNotExist(array $params)
103103
return $this;
104104
}
105105

106+
public function headerIs($name, $value)
107+
{
108+
$this->appendMatcher($value, $this->extractorFactory->createHeaderExtractor($name));
109+
110+
return $this;
111+
}
112+
113+
public function headerExists($name)
114+
{
115+
$this->appendMatcher(true, $this->extractorFactory->createHeaderExistsExtractor($name));
116+
117+
return $this;
118+
}
119+
106120
public function callback(Closure $callback)
107121
{
108122
$this->appendMatcher($this->matcherFactory->closure($callback));

src/Matcher/ExtractorFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,18 @@ public function createParamExistsExtractor($param)
4141
return $request->query->has($param);
4242
};
4343
}
44+
45+
public function createHeaderExtractor($header)
46+
{
47+
return static function (Request $request) use ($header) {
48+
return $request->headers->get($header);
49+
};
50+
}
51+
52+
public function createHeaderExistsExtractor($header)
53+
{
54+
return static function (Request $request) use ($header) {
55+
return $request->headers->has($header);
56+
};
57+
}
4458
}

tests/Matcher/ExtractorFactoryTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,72 @@ public function testGetPathWithBasePathThatDoesNotMatch()
8080
$extractor = $extractorFactory->createPathExtractor();
8181
$this->assertSame('', $extractor($this->request));
8282
}
83+
84+
public function testGetHeaderWithExistingHeader()
85+
{
86+
$request = new Request(
87+
[],
88+
[],
89+
[],
90+
[],
91+
[],
92+
['HTTP_CONTENT_TYPE' => 'application/json']
93+
);
94+
95+
$extractorFactory = new ExtractorFactory('/foo');
96+
97+
$extractor = $extractorFactory->createHeaderExtractor('content-type');
98+
$this->assertSame('application/json', $extractor($request));
99+
}
100+
101+
public function testGetHeaderWithNonExistingHeader()
102+
{
103+
$request = new Request(
104+
[],
105+
[],
106+
[],
107+
[],
108+
[],
109+
['HTTP_X_FOO' => 'bar']
110+
);
111+
112+
$extractorFactory = new ExtractorFactory('/foo');
113+
114+
$extractor = $extractorFactory->createHeaderExtractor('content-type');
115+
$this->assertSame(null, $extractor($request));
116+
}
117+
118+
public function testHeaderExistsWithExistingHeader()
119+
{
120+
$request = new Request(
121+
[],
122+
[],
123+
[],
124+
[],
125+
[],
126+
['HTTP_CONTENT_TYPE' => 'application/json']
127+
);
128+
129+
$extractorFactory = new ExtractorFactory('/foo');
130+
131+
$extractor = $extractorFactory->createHeaderExistsExtractor('content-type');
132+
$this->assertTrue($extractor($request));
133+
}
134+
135+
public function testHeaderExistsWithNonExistingHeader()
136+
{
137+
$request = new Request(
138+
[],
139+
[],
140+
[],
141+
[],
142+
[],
143+
['HTTP_X_FOO' => 'bar']
144+
);
145+
146+
$extractorFactory = new ExtractorFactory('/foo');
147+
148+
$extractor = $extractorFactory->createHeaderExistsExtractor('content-type');
149+
$this->assertFalse($extractor($request));
150+
}
83151
}

0 commit comments

Comments
 (0)