From 4491452e653508ba0a736aa32c7a93970aa08e26 Mon Sep 17 00:00:00 2001 From: Andy Vaughn Date: Sat, 27 Apr 2019 17:31:27 -0700 Subject: [PATCH] mock http headers without clearing out content type headers --- src/Mock/MockRequestMatcher.php | 12 ++++ .../withAddedHeaders_Test.php | 58 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/Mock/MockRequestMatcher/withAddedHeaders_Test.php diff --git a/src/Mock/MockRequestMatcher.php b/src/Mock/MockRequestMatcher.php index f6ecadb..84b4bae 100644 --- a/src/Mock/MockRequestMatcher.php +++ b/src/Mock/MockRequestMatcher.php @@ -149,6 +149,18 @@ public function withHeaders(IHeaders $headers) : MockRequestMatcher { return $request; } + /** + * Return an instance with the specified HTTP headers added to existing mocked HTTP headers. + * + * @param IHeaders $headers + * @return MockRequestMatcher + */ + public function withAddedHeaders(IHeaders $headers) : MockRequestMatcher { + $request = clone $this; + $request->headers->addHeaders($headers); + return $request; + } + /** * Return an instance with the specified body string * diff --git a/tests/Mock/MockRequestMatcher/withAddedHeaders_Test.php b/tests/Mock/MockRequestMatcher/withAddedHeaders_Test.php new file mode 100644 index 0000000..6e53a9f --- /dev/null +++ b/tests/Mock/MockRequestMatcher/withAddedHeaders_Test.php @@ -0,0 +1,58 @@ +withHeaders(Headers::newFromHeaderNameValuePairs([ + ['X-Qux', 'foo'] + ])) + ->withContent(new UrlEncodedFormDataContent([ + 'baz' => 'qux' + ])) + ->withAddedHeaders(Headers::newFromHeaderNameValuePairs([ + ['X-Fred', 'plugh'] + ])); + + // act + $result = $matcher->getHeaders(); + + // assert + $this->assertEquals([ + 'X-Qux: foo', + 'Content-Type: ' . ContentType::FORM_URLENCODED, + 'X-Fred: plugh' + ], $result->toRawHeaders()); + } +} \ No newline at end of file