public function createAnExpectationWithRegexReplacementFromUrl(AcceptanceTester $I) { $expectation = PhiremockClient::on(A::getRequest()->andUrl(Is::matching('/&test=(\\d+)/')))->then(Respond::withStatusCode(200)->andBody('the number is ${url.1}')); $this->phiremock->createExpectation($expectation); $I->sendGET('/potato', ['param1' => 123, 'test' => 456]); $I->seeResponseCodeIs('200'); $I->seeResponseContains('the number is 456'); }
public function createAnExpectationWithProxyToTest(AcceptanceTester $I) { $expectation = PhiremockClient::on(A::postRequest()->andUrl(Is::equalTo('/potato'))->andHeader('X-Potato', Is::sameStringAs('bAnaNa'))->andScenarioState('PotatoScenario', 'Scenario.START')->andBody(Is::equalTo('{"key": "This is the body"}')))->proxyTo('https://es.wikipedia.org/wiki/Proxy'); $this->phiremock->createExpectation($expectation); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs('200'); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":"PotatoScenario","scenarioStateIs":"Scenario.START",' . '"newScenarioState":null,"request":{"method":"post","url":{"isEqualTo":"\\/potato"},' . '"body":{"isEqualTo":"{\\"key\\": \\"This is the body\\"}"},"headers":{"X-Potato":' . '{"isSameString":"bAnaNa"}}},"response":{"statusCode":200,"body":null,"headers":' . 'null,"delayMillis":null},' . '"proxyTo":"https:\\/\\/es.wikipedia.org\\/wiki\\/Proxy","priority":0}]'); }
public function createSpecificationWithEmptyHeadersTest(AcceptanceTester $I) { $I->wantTo('create a specification with no headers in response'); $request = new Request(); $request->setUrl(new Condition('isEqualTo', '/the/request/url')); $response = new Response(); $specification = new Expectation(); $specification->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $specification); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":200,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]'); }
public function useDefaultWhenNoStatusCodeIsSetTest(AcceptanceTester $I) { $I->wantTo('fail when the status code is not set'); $request = new Request(); $request->setUrl(new Condition('isEqualTo', '/the/request/url')); $response = (new Response())->setStatusCode(null); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->seeResponseCodeIs(201); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs(200); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":200,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]'); }
public function returnCreatedExpectationTest(AcceptanceTester $I) { $request = new Request(); $urlCondition = new Condition('isEqualTo', '/the/request/url'); $request->setUrl($urlCondition); $response = new Response(); $response->setStatusCode(201); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs('200'); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"isEqualTo":"\\/the\\/request\\/url"},"body":null,"headers":null},' . '"response":{"statusCode":201,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]'); }
public function createAnExpectationUsingUrlMatchesTest(AcceptanceTester $I) { $I->wantTo('create an expectation that checks url using matches'); $request = new Request(); $request->setUrl(new Condition('matches', '/some pattern/')); $response = new Response(); $response->setStatusCode(201); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs('200'); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":null,"url":{"matches":"\\/some pattern\\/"},"body":null,"headers":null},' . '"response":{"statusCode":201,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]'); }
public function mockRequestWithDelayTest(AcceptanceTester $I) { $I->wantTo('mock a request with delay'); $request = new Request(); $request->setUrl(new Condition('isEqualTo', '/the/request/url'))->setMethod('GET'); $response = new Response(); $response->setDelayMillis(2000); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->seeResponseCodeIs(201); $start = microtime(true); $I->sendGET('/the/request/url'); $I->seeResponseCodeIs(200); $I->assertGreaterThan(2000, (microtime(true) - $start) * 1000); }
public function responseExpectedWhenSeveralHeadersMatchesTest(AcceptanceTester $I) { $I->wantTo('see if mocking based in several request headers works'); $request = new Request(); $request->setHeaders(['Content-type' => new Condition('isEqualTo', 'application/x-www-form-urlencoded'), 'X-Potato' => new Condition('matches', '/.*tomato.*/'), 'X-Tomato' => new Condition('isSameString', 'PoTaTo')]); $response = new Response(); $response->setBody('Found'); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->seeResponseCodeIs(201); $I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded'); $I->sendGET('/dontcare'); $I->seeResponseCodeIs(404); $I->haveHttpHeader('Content-Type', 'application/x-www-form-urlencoded'); $I->haveHttpHeader('X-potato', 'a-tomato-0'); $I->haveHttpHeader('X-tomato', 'potato'); $I->sendGET('/dontcare'); $I->seeResponseEquals('Found'); }
public function countExecutionsWhenNoExpectationIsSet(AcceptanceTester $I) { $I->sendDELETE('/__phiremock/executions'); $I->sendGET('/potato'); $I->seeResponseCodeIs(404); $I->sendGET('/potato'); $count = $this->phiremock->countExecutions(A::getRequest()->andUrl(Is::equalTo('/potato'))); $I->assertEquals(2, $count); $count = $this->phiremock->countExecutions(A::getRequest()->andUrl(Is::matching('~potato~'))); $I->assertEquals(2, $count); }
public function createAnExpectationUsingMethodHead(AcceptanceTester $I) { $I->wantTo('create a specification with method head'); $request = new Request(); $request->setMethod('head'); $response = new Response(); $response->setStatusCode(201); $expectation = new Expectation(); $expectation->setRequest($request)->setResponse($response); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs('200'); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":null,"scenarioStateIs":null,"newScenarioState":null,' . '"request":{"method":"head","url":null,"body":null,"headers":null},' . '"response":{"statusCode":201,"body":null,"headers":null,"delayMillis":null},' . '"proxyTo":null,"priority":0}]'); }
<?php $I = new AcceptanceTester($scenario); $I->am('Google Bot'); $I->wantTo('Test XML Sitemaps'); $I->expectTo('see the sitemap in robots.txt'); $I->sendGET('/robots.txt'); $I->seeResponseContains('Sitemap:'); $I->seeResponseContains('/sitemap.xml'); $I->seeResponseContains('/sitemapindex.xml'); $I->expectTo('see an XML sitemap index'); $I->sendGET('/sitemapindex.xml'); $I->seeResponseCodeIs(200); $I->haveHttpHeader('Content-Type', 'application/xml; charset=utf-8'); $I->seeResponseIsXml(); $I->dontSee('<b>Notice</b>'); $I->dontSee('error'); $I->expectTo('see a brief XML sitemap'); $I->sendGET('/sitemap.xml'); $I->seeResponseCodeIs(200); $I->seeResponseIsXml(); $I->haveHttpHeader('Content-Type', 'application/xml; charset=utf-8'); $I->seeResponseContains('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"'); $I->dontSee('<b>Notice</b>'); // error sometimes thrown if Wordpress timezone not set $I->dontSee('error'); $I->expectTo('see a paginated XML sitemap'); $I->sendGET('/sitemap.xml?page=1'); $I->seeResponseCodeIs(200); $I->seeResponseIsXml(); $I->haveHttpHeader('Content-Type', 'application/xml; charset=utf-8');
<?php /** * @var Codeception\Scenario $scenario */ $I = new AcceptanceTester($scenario); $I->wantTo('make sure that every page in the sitemap'); $I->sendGET('/sitemap'); $I->seeResponseIsXml(); $urls = $I->parseSitemap($I->grabResponse()); foreach ($urls as $url) { $I->amOnUrl($url); $I->seeResponseCodeIs(200); }
public function creationWithAllOptionsFilledTest(AcceptanceTester $I) { $I->wantTo('create an expectation with all possible option filled'); $request = (new Request())->setUrl(new Condition('isEqualTo', '/the/request/url'))->setBody(new Condition('isEqualTo', 'the body'))->setMethod('get')->setHeaders(['Content-Type' => new Condition('matches', '/json/'), 'Accepts' => new Condition('isEqualTo', 'application/json'), 'X-Some-Random-Header' => new Condition('isEqualTo', 'random value')]); $response = (new Response())->setStatusCode(201)->setBody('Response body')->setDelayMillis(5000)->setHeaders(['X-Special-Header' => 'potato', 'Location' => 'href://potato.tmt']); $expectation = (new Expectation())->setRequest($request)->setResponse($response)->setScenarioName('potato')->setScenarioStateIs('tomato')->setNewScenarioState('banana')->setPriority(3); $I->haveHttpHeader('Content-Type', 'application/json'); $I->sendPOST('/__phiremock/expectations', $expectation); $I->sendGET('/__phiremock/expectations'); $I->seeResponseCodeIs('200'); $I->seeResponseIsJson(); $I->seeResponseEquals('[{"scenarioName":"potato","scenarioStateIs":"tomato","newScenarioState":"banana",' . '"request":{' . '"method":"get","url":{"isEqualTo":"\\/the\\/request\\/url"},' . '"body":{"isEqualTo":"the body"},' . '"headers":{' . '"Content-Type":{"matches":"\\/json\\/"},' . '"Accepts":{"isEqualTo":"application\\/json"},' . '"X-Some-Random-Header":{"isEqualTo":"random value"}}},' . '"response":{' . '"statusCode":201,"body":"Response body","headers":{' . '"X-Special-Header":"potato",' . '"Location":"href:\\/\\/potato.tmt"},' . '"delayMillis":5000},' . '"proxyTo":null,"priority":3}]'); }
$app = Rest\App::instance(); $app->addResource($ur, Examples\User::class, $urx); $app->addResource($tr, Examples\Token::class, $trx); $app->addResource($ar, Examples\Article::class, $arx); $app->addResource($cr, Examples\Comment::class, $crx); $app->handle(); //////////////////////////////////////////////////////////////////////////////// $I = new AcceptanceTester($scenario); $foo = 'foo'; $fooP = 'barbarbar'; $I->wantTo("Create create a user {$foo}"); $I->sendPOST('/users', ['username' => $foo, 'password' => 'bar']); $I->seeResponseCodeIs(Rest\Response::BAD_REQUEST); $I->seeHttpHeader('Status', '400 Password too short'); $I->sendPOST('/users', ['username' => $foo, 'password' => $fooP]); $I->seeResponseCodeIs(201); $I->seeHttpHeader('Location', "/users/{$foo}"); $I->seeResponseEquals(json_encode(['username' => $foo, 'password' => sha1($fooP)])); $fooUrl = $I->grabHttpHeader('Location'); $I->wantTo('See the new user'); $I->sendGET($fooUrl); $I->seeResponseCodeIs(200); $I->seeResponseEquals(json_encode(['username' => $foo, 'login' => "/users/{$foo}/token", 'articles' => "/users/{$foo}/articles"])); $rsp = json_decode($I->grabResponse()); $fooLogin = $rsp->login; $fooArticles = $rsp->articles; $I->wantTo("login with {$foo}"); $I->sendPOST($fooLogin, ['username' => $foo, 'password' => sha1($fooP)]); $I->seeResponseCodeIs(201); $rsp = $I->grabResponse(); $fooToken = $rsp['token'];
<?php /** * @var Codeception\Scenario $scenario */ // We have rewrite rule for Nginx // robots.txt => robots $I = new AcceptanceTester($scenario); $I->wantTo('make sure that the robots.txt is exists'); $I->sendGET('/robots'); $I->seeResponseCodeIs(200); $pattern = '#^User-agent: \\*\\nAllow: \\/\\nSitemap: https?:\\/\\/.+\\/sitemap$#'; $I->seeResponseRegexp($pattern, $I->grabResponse());