I wrote some PHP Unit Tests, that need User Authentication for a Request.
For that i added some parameterse to createClient:
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => TEST_USER_NAME,
'PHP_AUTH_PW' => TEST_USER_PASS,
));
TEST_USER_NAME and TEST_USER_PASS containing the Login Credentials.
If I do a request like that
$parameters = array(
"object" => self::TEST_OBJECT_ID,
);
$headers = array(
'HTTP_API_AUTHORIZATION' => 'API_AUTH_KEY',
);
$this->client->request('POST', '/api/v4/object/get', $parameters, array(), $headers);
$response = $this->client->getResponse();
$this->assertEquals(200, $response->getStatusCode(), $response->getContent());
The test says OK, but after that this Message appears:
THE ERROR HANDLER HAS CHANGED!
If i change the Credentials to something wrong, the message does not appear.
Any suggestions how i can prevent that or remove this message?
Found my mistake - i browsed all tests and regarding code and it was was the error said, i changed the error handler.
set_error_handler(array(&$this, 'handleGeoError'));
Related
I am trying to redirect user to different url after certain event has fired but I keep receiving this error:
Symfony\Component\HttpFoundation\Response::setContent(): Argument #1 ($content) must be of type ?string, Illuminate\Routing\Redirector given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Http/Response.php on line 72
I have event listener in EventServiceProvider:
\Slides\Saml2\Events\SignedIn::class => [
SamlLoginListener::class.'#handle',
]
and then in SamlLoginListener:
public function handle(SignedIn $event)
{
$messageId = $event->auth->getLastMessageId();
// your own code preventing reuse of a $messageId to stop replay attacks
$samlUser = $event->auth->getSaml2User();
$user_data = [
'id' => $samlUser->getUserId(),
'attributes' => $samlUser->getAttributes(),
'assertion' => $samlUser->getRawSamlAssertion()
];
AuthController::saml_login($user_data);
}
Then In the AuthController::saml_login I tried all of these but the response was allways the same error:
return redirect()->away($frontend_url, 302, [
'X-SSO-AUTH-TOKENS' => json_encode($data),
]);
//
$response = new RedirectResponse($redirect_url, 302, [
'X-SSO-AUTH-TOKENS' => json_encode($data),
]);
return $response;
//
return redirect($redirect_url, 302, [
'X-SSO-AUTH-TOKENS' => json_encode($data),
]);
I decided to try it again by just returning 'ok' but still received the same error. I tried clearing the cache - no result.
Here is the full error trace: https://pastebin.com/4eZYni0w
The answer to the problem is in the error message. Your error is saying that the redirect helper method expects a string, but you're passing an object.
Not sure what version of Laravel you're using but in Laravel, Redirect responses are instances of the Illuminate\Http\RedirectResponse class and contain the proper headers needed to redirect the user to another URL.
you can try:
return redirect('/frontend_route');
Redirect to named route:
return redirect()->route('frontend_route');
If this your route has parameters like in your case, here's what to do:
return redirect()->route('frontend_route', ['X-SSO-AUTH-TOKENS' => json_encode($data)]);
Hope this helps.
source: Redirects in Laravel 8
I ended up doing this and it worked:
header("Location: " . $redirect_url);
header("X-SSO-AUTH-TOKENS: " . json_encode($data));
exit();
I have a test which is trying to login and get details. I need to pass a bearer token in request header for this operation. Please see the below code. I could see that the header seldom has the headers that I set. Can anyone give me a pointer to fix this issue?
I am using Laravel 7.2.2, PHP 7.4,
And I am running php artisan test
Code:
public function a_user_can_get_details()
{
$this->create_user();
$response = $this->json('POST',
'/api/login',
[
"email" => "john.doe#example.com",
"password" => "john123"
]);
$response->assertSuccessful();
$token = Str::replaceLast('"', '', Str::replaceFirst('"', '', $response->getContent()));
$headers = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token
];
$response = $this->withHeaders($headers)
->get('api/user');
$response->assertSuccessful();
$this->assertCount(1, User::all());
}
And here is the error I am getting. Actually, the test must pass. That is the right user name and password:
Response status code [403] is not a successful status code. Failed asserting that false is true.
at tests/Feature/UserTest.php:141
137|
138| $response = $this->withHeaders($headers)
139| ->get('api/user');
140|
> 141| $response->assertSuccessful();
142| $this->assertCount(1, User::all());
143|
144| }
145|
I solved this issue.
Cause: I had added a middleware condition in Spatie Permissions to check for a permission for that specific route. I had to remove that to get this working.
Instead of that, I now, check the logged in status of the user. So that the route could be hit and needed checks are done inside the route.
Thanks all for the questions/comments which helped me to solve this.
I am trying to connect my script to the SOAP client. But when I try to do it throws the mentioned error. When I tried to get the function with
$client->__getFunctions(). It shows all the function. when I try to call them it ends in fatal error.
$client = new SoapClient("http://bsestarmfdemo.bseindia.com/MFOrderEntry/MFOrder.svc?singleWsdl",array(
'soap_version' => SOAP_1_2, // !!!!!!!
));
var_dump($client->__getFunctions());
//var_dump($client->__getTypes());
$login_params = array(
'UserId' => 123456,
'Password' => 123456,
'PassKey' => 1234569870,
);
//$response = $client->getPassword($login_params);
$response = $client->__soapCall('getPassword', array($login_params));
dd($response);
if i change the SOAP version to 1.1 i get another error Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. Would be great if i come to know what i am missing here.
To address the reply made by #Naveen Kumar: this was my solution.
// Apply WSA headers
$action = new \SoapHeader('http://www.w3.org/2005/08/addressing', 'Action', self::ACTION_PREFIX.$this->action);
$to = new \SoapHeader('http://www.w3.org/2005/08/addressing', 'To', $this->endpoint);
$this->client()->__setSoapHeaders([$action, $to]);
In the end, $this->client simply returns an instance of \SoapClient and the action contains the full URL to the method. Naturally, the To portion is optional, but in my case it obviously points towards the endpoint specified in the .wsdl file.
I'm new to Laravel framework so I'm having a hard time to do something very trivial. The main idea is to contact an API and get its response. Below is my function where I'm having error,
public function verification($id=null){
try{
$res = $client->createRequest('POST','http://35.161.181.102/api/socialverify/linkedin',['headers' => $headers , 'body' => $urlclean]);
$res= $client->send($res);
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
}
When I run the above function I'm getting the error shown below,
Illegal string offset 'id'
Any pointers on what I'm doing wrong and how can I solve it.
Any help is appreciated. Thank in advance.
What do you see in your /storage/logs/laravel.log?
I assume Client is Guzzle Client and by default Guzzle throws RequestException whenever there is a request issue. See Documentation. So why not try to do this and see what's the error responded from Guzzle:
try {
$response = $client->post('http://link-to-my-api', array(
'headers' => array('Content-type' => 'application/json'),
'body' => $data
));
$response->send();
}catch(\GuzzleHttp\Exception\RequestException $e) {
\Log::info($e->getMessage());
\Log::info($e->getCode());
\Log::info($e->getResponse()->getBody()->getContents());
}
And check your /storage/logs/laravel.log to see the logs being printed.
you can try this way:
use Illuminate\Support\Facades\Http;
$response = Http::withHeaders($header)
->post($url, [
$data
]);
I am trying to implement a soaprequest and making the call does seem to work. The only problem is: I don't know how to receive the response data. My code looks like this:
$auth = array(
'UsernameToken' => array(
'Username' => 'xxx',
'Password' => 'yyyy'
)
);
$header = new SoapHeader('xs','Security',$auth, 0);
$client->__setSoapHeaders($header);
$client->__setLocation('http://example.com/test.php');
$params = array(
...
'trace' => 1,
'cache_wsdl' => 0
);
try {
$response = $client->getSomeData($params);
}catch(Exception $e){
echo "Exception: ".$e->getMessage();
}
print_r($response);
This results in an empty page, because $response is empty. But the test.php file is called (I tried with a simple mail() command and it sends the mail every time I call the page with the soapclient).
So I guess the soap response is somehow sent to the test.php file - right? How do I get it? If I do not set the location, I get a nullpointerexception, so I have to do that. I tried
$client->__getLastResponse()
that's empty too.
What can I do, how do I get the soap response data? Any hints would be appreciated. Thank you!