Skip to content

Commit

Permalink
Support proxy prefix removal
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanhill committed Dec 31, 2021
1 parent 2e61a2b commit df79a1f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/WireMock/Client/ResponseDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class ResponseDefinitionBuilder
private $_transformerParameters = array();

protected $_additionalRequestHeaders = array();
/** @var string */
protected $_proxyUrlPrefixToRemove;

/**
* @param int $status
Expand Down Expand Up @@ -222,7 +224,8 @@ public function build()
$this->_chunkedDribbleDelay,
$this->_fault,
$this->_transformers,
$this->_transformerParameters
$this->_transformerParameters,
$this->_proxyUrlPrefixToRemove
);
}
}
Expand Down Expand Up @@ -250,4 +253,14 @@ public function withAdditionalRequestHeader($headerName, $value)
$this->_additionalRequestHeaders[$headerName] = $value;
return $this;
}

/**
* @param string $proxyUrlPrefixToRemove
* @return $this
*/
public function withProxyUrlPrefixToRemove($proxyUrlPrefixToRemove)
{
$this->_proxyUrlPrefixToRemove = $proxyUrlPrefixToRemove;
return $this;
}
}
20 changes: 18 additions & 2 deletions src/WireMock/Http/ResponseDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ResponseDefinition
private $_transformers = array();
/** @var array */
private $_transformerParameters = array();
/** @var string */
private $_proxyUrlPrefixToRemove;

/**
* ResponseDefinition constructor.
Expand Down Expand Up @@ -68,7 +70,8 @@ public function __construct(
$chunkedDribbleDelay,
$fault,
$transformers,
$transformerParameters
$transformerParameters,
$proxyUrlPrefixToRemove
) {
$this->_status = $status;
$this->_statusMessage = $statusMessage;
Expand All @@ -84,6 +87,7 @@ public function __construct(
$this->_transformers = $transformers;
$this->_transformerParameters = $transformerParameters;
$this->_additionalProxyRequestHeaders = $additionalProxyRequestHeaders;
$this->_proxyUrlPrefixToRemove = $proxyUrlPrefixToRemove;
}

/**
Expand Down Expand Up @@ -198,6 +202,14 @@ public function getTransformerParameters()
return $this->_transformerParameters;
}

/**
* @return string
*/
public function getProxyUrlPrefixToRemove()
{
return $this->_proxyUrlPrefixToRemove;
}

public function toArray()
{
$array = array();
Expand Down Expand Up @@ -241,6 +253,9 @@ public function toArray()
if ($this->_transformerParameters) {
$array['transformerParameters'] = $this->_transformerParameters;
}
if ($this->_proxyUrlPrefixToRemove) {
$array['proxyUrlPrefixToRemove'] = $this->_proxyUrlPrefixToRemove;
}
return $array;
}

Expand Down Expand Up @@ -271,7 +286,8 @@ public static function fromArray(array $array)
null,
isset($array['fault']) ? $array['fault'] : null,
isset($array['transformers']) ? $array['transformers'] : null,
isset($array['transformerParameters']) ? $array['transformerParameters'] : null
isset($array['transformerParameters']) ? $array['transformerParameters'] : null,
isset($array['proxyUrlPrefixToRemove']) ? $array['proxyUrlPrefixToRemove'] : null
);
}
}
14 changes: 14 additions & 0 deletions test/WireMock/Integration/ProxyingIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,18 @@ public function testAdditionProxiedRequestHeadersCanBeSet()
assertThat($stubMappingArray['response']['additionalProxyRequestHeaders'], equalTo(array('X-Header' => 'val')));
assertThatTheOnlyMappingPresentIs($stubMapping);
}

public function testProxyUrlPrefixToRemoveCanBeSet()
{
// when
$stubMapping = self::$_wireMock->stubFor(WireMock::get(WireMock::urlEqualTo("/other/service/doc/123"))
->willReturn(WireMock::aResponse()
->proxiedFrom("http://otherhost.com/approot")
->withProxyUrlPrefixToRemove("/other/service")));

// then
$stubMappingArray = $stubMapping->toArray();
assertThat($stubMappingArray['response']['proxyUrlPrefixToRemove'], equalTo('/other/service'));
assertThatTheOnlyMappingPresentIs($stubMapping);
}
}

0 comments on commit df79a1f

Please sign in to comment.