Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HttpClient] Add MockResponse::getRequestMethod() and getRequestUrl() to allow inspecting which request has been sent#36487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromjavespi:addUrlHttpMethodInMockResponse
Jul 1, 2020

Conversation

@javespi
Copy link
Contributor

@javespijavespi commentedApr 17, 2020
edited
Loading

QA
Branch?4.4
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

As same as theMockResponse class hasgetRequestOptions() when doing a request; I've added:

  • getRequestUrl() - returns the URL used when doing the request
  • getRequestMethod() - returns the HTTP method used when doing the request

With these two getters, we would be able to assert that the method and URL passed to theHttpClient were well generated. I've tried to assert the URL generated in a unit test of a class with aSymfonyHttpClient injected and it wasn't possible. Calling$mock->getInfo('url') returnsnull.

Example, if we have a class withHttpClientInterface injected like this:

finalclass SymfonyHttpUserClient{privateHttpClientInterface$httpClient;privatestring$baseUri;publicfunction__construct(HttpClientInterface$httpClient,string$baseUri    ) {$this->httpClient =$httpClient;$this->baseUri =$baseUri;    }publicfunctiongetById(string$userId):void    {$this->httpClient->request('GET',$this->baseUri .$customerId        );    }}

And if we want to do a unit test ofSymfonyHttpUserClient right now we are not able to check if the URL of the request was well-formed passing aMockResponse. Also, we weren't able to assert the HTTP method (maybe this piece is not so critical to assert in the unit test).

class SymfonyHttpUserClientTestsextends TestCase{publicfunctiontestGetById():void    {$baseUri ='https://user-api.test/api/v1/users/';$mockResponse =newMockResponse();$symfonyHttpUserClient =newSymfonyHttpUserClient(newMockHttpClient(                [$mockResponse],$baseUri            ),$baseUri        );// test get by id:$symfonyHttpUserClient->getById('some-user-id');// cannot be asserted right now:$this->assertSame($mockResponse->getInfo('url'),$baseUri .'some-user-id');// fail$this->assertSame($mockResponse->getInfo('http_method'),'GET');// fail// it could be with the new getters:$this->assertSame($mockResponse->getRequestUrl(),$baseUri .'some-user-id');$this->assertSame($mockResponse->getRequestMethod(),'GET');    }}

This only happens if the class has injected the HttpClient and if it is used inside void methods with no being able to return the response. If the class returns the response,url andhttp_method are available with$response->getInfo() call. But this response object is a new one, is not the mock passed by argument when you instance the MockHttpClient.

Var dumps ofgetInfo array:

$response->getInfo() from MockClient:..array(10) {  ["start_time"]=>  float(1587109014.7985)  ["user_data"]=>  NULL  ["http_code"]=>  int(200)  ["response_headers"]=>  array(0) {  }  ["error"]=>  NULL  ["canceled"]=>  bool(false)  ["redirect_count"]=>  int(0)  ["redirect_url"]=>  NULL  ["http_method"]=>  string(3) "GET"  ["url"]=>  string(47) "https://user-api.test/api/v1/users/some-user-id"}$mock->getInfo()array(4) {  ["http_code"]=>  int(200)  ["response_headers"]=>  array(0) {  }  ["error"]=>  NULL  ["canceled"]=>  bool(false)}

This is a minor change, I opened the PR with4.4 as base branch; but not sure if it should be opened with5.0 as base branch or evenmaster taking account is a feature (add two new getters in MockResponse class). Let me know if I didn't follow right the instructions (first PR on Symfony project 😃)

Thanks!

@nicolas-grekas
Copy link
Member

Would it make sense to do$mock->info = &$response->info; instead? That would allow checking any option from the response when having the mock at hand, viagetInfo(). Can you give it a try?

javespi reacted with thumbs up emoji

@nicolas-grekasnicolas-grekas added this to thenext milestoneApr 18, 2020
@javespi
Copy link
ContributorAuthor

Hi@nicolas-grekas, is there anything I can do to move forward this PR? Thanks! 😃

… to allow inspecting which request has been sent
@nicolas-grekasnicolas-grekas changed the title[HttpClient][MockResponse] Add HTTP method and URL of the request[HttpClient] Add MockResponse::getRequestMethod() and getRequestUrl() to allow inspecting which request has been sentJun 30, 2020
@nicolas-grekasnicolas-grekas changed the base branch from4.4 tomasterJune 30, 2020 16:39
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

(I just rebased the PR to target master)

javespi reacted with thumbs up emoji
@fabpot
Copy link
Member

Thank you@javespi.

@fabpotfabpot merged commitd8082fa intosymfony:masterJul 1, 2020
@javespijavespi deleted the addUrlHttpMethodInMockResponse branchJuly 1, 2020 13:19
@javespi
Copy link
ContributorAuthor

Thanks@fabpot@nicolas-grekas for all the effort and support!!

@nicolas-grekasnicolas-grekas modified the milestones:next,5.2Oct 5, 2020
@fabpotfabpot mentioned this pull requestOct 5, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@fabpotfabpotfabpot approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

5.2

Development

Successfully merging this pull request may close these issues.

4 participants

@javespi@nicolas-grekas@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp