Skip to content

Commit

Permalink
mock http headers without clearing out content type headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Vaughn committed Apr 28, 2019
1 parent e1b6263 commit 4491452
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Mock/MockRequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
58 changes: 58 additions & 0 deletions tests/Mock/MockRequestMatcher/withAddedHeaders_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/**
* MindTouch HTTP
* Copyright (C) 2006-2018 MindTouch, Inc.
* www.mindtouch.com [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace MindTouch\Http\tests\Mock\MockRequestMatcher;

use MindTouch\Http\Content\ContentType;
use MindTouch\Http\Content\UrlEncodedFormDataContent;
use MindTouch\Http\Headers;
use MindTouch\Http\HttpPlug;
use MindTouch\Http\Mock\MockRequestMatcher;
use MindTouch\Http\tests\MindTouchHttpUnitTestCase;
use MindTouch\Http\XUri;

class withAddedHeaders_Test extends MindTouchHttpUnitTestCase {

/**
* @test
*/
public function Can_add_headers() {

// arrange
$matcher = (new MockRequestMatcher(HttpPlug::METHOD_POST, XUri::tryParse('http://example.com/bazz')))
->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());
}
}

0 comments on commit 4491452

Please sign in to comment.