Created
July 15, 2016 22:54
-
-
Save fdiasr/c4e9774bae487e4884c818509c2690e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Ingresse\Behat\ApiExtension\Context; | |
| use GuzzleHttp\Psr7\Request; | |
| use Behat\Gherkin\Node\PyStringNode; | |
| use Behat\Gherkin\Node\TableNode; | |
| use GuzzleHttp\Exception\RequestException; | |
| trait RequestContextTrait | |
| { | |
| /** | |
| * @param string $url relative url | |
| * | |
| * @When I make request to :arg1 | |
| */ | |
| public function iMakeGetRequest($uri) | |
| { | |
| $uri = $this->prepareUrl($uri); | |
| $this->request = new Request('GET', $uri, $this->headers); | |
| $this->sendRequest(); | |
| } | |
| /** | |
| * @param string $method request method | |
| * @param string $url relative url | |
| * @param PyStringNode $json request body | |
| * | |
| * @When /^I make "([A-Z]+)" request to "([^"]+)" with json data:$/ | |
| */ | |
| public function iMakeRequestWithJson($method, $url, PyStringNode $json) | |
| { | |
| $url = $this->prepareUrl($url); | |
| $string = $this->replacePlaceHolder(trim($json)); | |
| $this->request = new Request($method, $url, $this->headers, $json); | |
| $this->sendRequest(); | |
| } | |
| /** | |
| * @When I request :request | |
| */ | |
| public function iMakeRequest($request, PyStringNode $json) | |
| { | |
| $verb = 'GET'; $url = '/'; | |
| list($verb, $url) = explode(" ", $request); | |
| $url = $this->prepareUrl($url); | |
| if ($verb != 'GET') { | |
| $json = $this->prepareJson($json); | |
| } | |
| $this->request = new Request($method, $url, $this->headers, $json); | |
| $this->sendRequest(); | |
| } | |
| /** | |
| * @param string $method request method | |
| * @param string $url relative url | |
| * @param TableNode $post table of post values | |
| * | |
| * @When /^I make "([A-Z]+)" request to "([^"]+)" with form data:$/ | |
| */ | |
| public function iMakeRequestWithForm($method, $url, TableNode $post) | |
| { | |
| $url = $this->prepareUrl($url); | |
| $formHeader = ['Content-Type' => 'application/x-www-form-urlencoded']; | |
| $headers = array_merge($this->headers, $formHeader); | |
| $fields = ''; | |
| foreach ($post as $item) { | |
| $fields .= sprintf('%s=%s', $item['field'], $item['value']); | |
| } | |
| $this->request = new Request($method, $url, $headers, $fields); | |
| $this->sendRequest(); | |
| } | |
| /** | |
| * @param string $name header name | |
| * @param string $value header value | |
| * | |
| * @Given /^I set header "([^"]*)" with value "([^"]*)"$/ | |
| */ | |
| public function iSetHeaderWithValue($name, $value) | |
| { | |
| $this->addHeader($name, $value); | |
| } | |
| private function sendRequest() | |
| { | |
| try { | |
| $this->response = $this->client->send($this->request); | |
| } catch (RequestException $e) { | |
| $this->response = $e->getResponse(); | |
| if (null === $this->response) { | |
| throw $e; | |
| } | |
| } | |
| } | |
| /** | |
| * @param string $url | |
| * @return string | |
| */ | |
| private function prepareUrl($url) | |
| { | |
| return ltrim($this->prepareData($url), '/'); | |
| } | |
| /** | |
| * @param string $url | |
| * @return string | |
| */ | |
| private function prepareJson($json) | |
| { | |
| return $this->prepareData($json); | |
| } | |
| private function prepareData($data) | |
| { | |
| foreach ($this->placeHolders as $key => $val) { | |
| $data = str_replace($key, $val, $data); | |
| } | |
| return $data; | |
| } | |
| /** | |
| * @param string $key token name | |
| * @param string $value replace value | |
| */ | |
| public function setPlaceHolder($key, $value) | |
| { | |
| $this->placeHolders[$key] = $value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment