# |
Jun 12th 2021, 18:56 |
kevin.pfeifer |
if its better/worse or something else I have no idea |
# |
Jun 12th 2021, 18:56 |
kevin.pfeifer |
I like doing that ```$response = $this->getResponse(); $this->autoRender = false; return $response->withType( 'application/json' ) ->withStringBody( json_encode( $results ) );``` |
# |
Jun 12th 2021, 18:54 |
davinci |
I've always used a view, but see someone using an exit, and thought... hmm, maybe that's faster? |
# |
Jun 12th 2021, 18:54 |
davinci |
In an ajax call, is it better to echo json data then exit right from the controller, or use an ajax view? |
# |
Jun 11th 2021, 15:30 |
admad |
Mention that HTTP_HOST was set through phpunit.xml |
# |
Jun 11th 2021, 15:28 |
admad |
@a.vanniel plz open an issue on the cakephp/app repo. |
# |
Jun 11th 2021, 15:28 |
admad |
We could modify that line to first check for HTTP_HOST and use "localhost" only of it's not available. |
# |
Jun 11th 2021, 15:15 |
ndm |
Sure, generally these settings would allow that, but CakePHP does what CakePHP does. Respecting those configs would probably cause a lot of trouble with people that touch the superglobals directly, being in their apps or in their tests. Anyways, godspeed :upside_down_face: |
# |
Jun 11th 2021, 15:10 |
a.vanniel |
Thanks @ndm! |
# |
Jun 11th 2021, 15:10 |
a.vanniel |
I have commented out the offending line though and now the test succeeds because the expected url was set... |
# |
Jun 11th 2021, 15:10 |
a.vanniel |
At least, that is what I understood of the xml file... |
# |
Jun 11th 2021, 15:09 |
a.vanniel |
Using the phpunit.xml those super globals could be set and thus have the tests approach more closely the sort of real life situations... |
# |
Jun 11th 2021, 15:08 |
ndm |
Probably... I think all the globals for the server request object are being filled with custom data in integration tests, ie the PHP supergobals are not being touched. |
# |
Jun 11th 2021, 15:06 |
a.vanniel |
or it actually defeats the purpose of the server variable as well. |
# |
Jun 11th 2021, 15:06 |
a.vanniel |
It kinda defeats the purpose of the HTTP_HOST environment key in the phpunit.xml I guess. |
# |
Jun 11th 2021, 15:06 |
ndm |
I think there's also places internally in the core where `App.fullBaseUrl` is being overwritten... so, you might not be off the hook if you'd wanted to use your custom host setting. |
# |
Jun 11th 2021, 15:04 |
ndm |
Actually, yes, it's the default :) Didn't remember that one. |
# |
Jun 11th 2021, 15:04 |
ndm |
Good question.... |
# |
Jun 11th 2021, 15:03 |
a.vanniel |
Is that per default? Or do I have to start kicking one of my colleagues here? |
# |
Jun 11th 2021, 15:02 |
a.vanniel |
wow. |
# |
Jun 11th 2021, 15:02 |
a.vanniel |
wait... I found it in the tests\bootstrap.php file... |
# |
Jun 11th 2021, 15:02 |
a.vanniel |
I did a debug of env('HTTP_HOST') in the controller I am testing and I get this: http://pearson.presenter.local so that is what it should be, |
# |
Jun 11th 2021, 15:00 |
a.vanniel |
bootstrap_cli.php does not overwrite it.. that line is commented out it only contains the lines to configure the debug and error log file names. |
# |
Jun 11th 2021, 14:59 |
ndm |
So `env('HTTP_HOST)` returns the expected result? Then maybe check if the option is being overwritten in `bootstrap_cli.php` |
# |
Jun 11th 2021, 14:58 |
a.vanniel |
FYI the fullBaseUrl setting in the config file of the application is set to false... so this should use the HTTP_HOST |
# |
Jun 11th 2021, 14:57 |
a.vanniel |
welll. this is rather weird... $ref is set to the url I provided, however the $base is set to http://localhost eventhough I have set this in the phpunit.xml: ```<server name="HTTP_HOST" value="http://pearson.presenter.local" />``` and this actually does work ... so I wonder why $base would be set to localhost ... and since the default is to return a local address... well, this probably fails since the base is different... |
# |
Jun 11th 2021, 14:54 |
ndm |
And I guess debug `$ref` too while you're at it, just to make sure a value is actually set. |
# |
Jun 11th 2021, 14:53 |
ndm |
Ok, now try a `debug($base)` in the `referer()` method to see what is being expected. |
# |
Jun 11th 2021, 14:51 |
a.vanniel |
I know, environment should not be set since it is a header setting... but just to be sure :) |
# |
Jun 11th 2021, 14:51 |
slackebot |
'http://pearson.presenter.local/mediafiles/view/6', ], ]);``` still the $this->referer() in the controller to be tested, results in '/' |
# |
Jun 11th 2021, 14:51 |
a.vanniel |
Just so I was sure I had set all of them, I did these right before the $this->get() in the test: ```$this->configRequest([ 'headers' => [ 'HTTP_REFERER' => 'http://pearson.presenter.local/mediafiles/view/6', 'REFERER' => 'http://pearson.presenter.local/mediafiles/view/6', ], 'environment' =>[ 'HTTP_REFERER' => 'http://pearson.presenter.local/mediafiles/view/6', 'REFERER' => |
# |
Jun 11th 2021, 14:50 |
ndm |
Well do both, an absolute and a header without `HTTP_`. Then make sure that the full URL that you are providing starts with what is set for `App.fullBaseUrl`. It might be different to what you expect in your tests. |
# |
Jun 11th 2021, 14:47 |
a.vanniel |
it keeps giving me '/' as the referer... |
# |
Jun 11th 2021, 14:47 |
a.vanniel |
Unfortunately setting the referer key to the absolute url did not seem to change anything $this->referer() is returning in the controller. |
# |
Jun 11th 2021, 14:45 |
kevin.pfeifer |
we literally talked about that yesterday :) |
# |
Jun 11th 2021, 14:45 |
ndm |
Well, maybe not in any case... but at least when `App.fullBaseUrl` is set to a URL. |
# |
Jun 11th 2021, 14:45 |
slackebot |
<kevin.pfeifer> |
# |
Jun 11th 2021, 14:44 |
ndm |
Don't use the `HTTP_` prefix in headers, it will be added automatically where required. That being said, passing the full name via `environment` should work too, but in any case you need to use absolute URLs. |
# |
Jun 11th 2021, 14:40 |
a.vanniel |
I at first tried this: ```$this->configRequest([ 'headers' => ['HTTP_REFERER' => '/mediafiles/view/6'], ]);``` but that did not change anything and after digging in how cake determines the $this->referer()I figured it could be an environment setting ... seeing this is inside the ServerRequest::referer() method: ```public function referer(bool $local = true): ?string { $ref = $this->getEnv('HTTP_REFERER');``` |
# |
Jun 11th 2021, 14:37 |
greg138 |
I think you want to set the Referer header rather than environment? |
# |
Jun 11th 2021, 14:19 |
a.vanniel |
Good afternoon everyone. Does anyone potentially know how to set the http_referer for unit testing? I have this code that acts differently depending on the origin (referrer) and I though this would be the sollution but it does not seem to work: ```$this->configRequest([ 'environment' => ['HTTP_REFERER' => '/mediafiles/view/6'], ]);``` instead the default '/' remains set as the referrer. |