I have function return JSON
return response()->json([
'status' => 'OK',
'data' => [
'id' => $routeForecast->id,
],
])
how can I make test for this .. my laravel version is 5.1 and i used assertJson([ ]) it give me this error
***** PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertJson() must be a string
and try seeJson it give me this error
***** Invalid JSON was returned from the route. Perhaps an exception was thrown?
how can i solve it ??
Try something like this.
// When
$response = $this->getJson( 'v1/helloWorld' );
//dd( $response ); // perhaps die and dump here to check response?
// Then
$response
->assertStatus( 200 )
->assertJson( [
'status' => 'OK',
] );
Related
I'm trying to make post request and the post request is working but I'm not getting the response
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Basic ' . 'token==']]);
$data = $client->post(
'url',
[
'form_params' => [
'address' => 'addreww',
'name' => 'Zia Sultan',
'phone_number' => '2136000000',
]
]
);
return $data;
What I'm getting in my insomnia
Error: Failure when receiving data from the peer
You're code is working, post method returns ResponseInterface, we need to fetch the content from it, we need to first fetch the StreamInterface by calling getBody() and chaining it will getContents() gives us the actual response. Keep debug mode On, to get find the exact error for Error: Failure when receiving data from the peer, and when this error occurs, share the entire trace with us
try {
$response = (new \GuzzleHttp\Client())->post(
'url',
[
'headers' => [
'Authorization' => 'Basic ' . 'token=='
],
'form_params' => [
'address' => 'addreww',
'name' => 'Zia Sultan',
'phone_number' => '2136000000',
],
// 'http_errors' => false, // Set to false to disable throwing exceptions on an HTTP protocol errors (i.e., 4xx and 5xx responses)
// 'debug' => true,
// 'connect_timeout' => 30 // number of seconds to wait while trying to connect to a server, Use 0 to wait indefinitely (the default behavior)
// 'read_timeout' => 10 // timeout to use when reading a streamed body, default value is default_socket_timeout in php.ini
// 'timeout' => 30 // the total timeout of the request in seconds. Use 0 to wait indefinitely (the default behavior).
]
);
return $response->getBody()->getContents();
} catch (Throwable $exception) {
print_r($exception);
}
I was returning the data only but I needed to return getBody() like this way
$data->getBody()
Its working now
I have the following function, but when I run it in Postman to see the result, it doesn't print any value to me, it doesn't even give me an error. The var_dump set if it detects them, but the array does not... I think there is something wrong in the method updateOrCreate , because when I print this variable with var_dump, I can't see anything in the console.
This is the function:
public function createBidRival(Request $request)
{
$response = array('code' => 400, 'error_msg' => []);
if (!$request->id_karatekas) array_push($response['error_msg'], 'id_karateka is required');
if (!$request->id_participant_bid_send ) array_push($response['error_msg'], 'id_participant_bid_send is required');
if (!$request->id_participant_bid_receive) array_push($response['error_msg'], ' id_participant_bid_receive is required');
if (!$request->bid_rival) array_push($response['error_msg'], 'bid rival is required');
if (!count($response['error_msg']) > 0) {
try {
var_dump($request->id_karatekas);
var_dump($request->id_participant_bid_send);
var_dump($request->id_participant_bid_receive);
var_dump($request->bid_rival);
$bidRival = new BidBetweenRivals();
$bidRival = BidBetweenRivals::updateOrCreate(
[
'id_participant_bid_send' => $request->id_participant_bid_send,
'id_participant_bid_receive' => $request->id_participant_bid_receive,
'id_karatekas' => $request->id_karatekas
],
[
'id_participant_bid_send' => $request->id_participant_bid_send,
'id_participant_bid_receive' => $request->id_participant_bid_receive,
'id_karatekas' => $request->id_karatekas,
'bid_rival' => $request->bid_rival
]
);
$bidBetweenRivals->save;
$response = array('code' => 200, 'bidBetweenRivals' => $bidRival, 'msg' => 'Bid created');
}catch(\Exception $exception) {
$response = array('code' => 500, 'error_msg' => $exception->getMessage());
}
}
}
Dump to see whether if (!count($response['error_msg']) > 0) true or not and also dump something in the catch block to see if exception is occurring or not.
You can also test by commenting out the updateOrCreate part to see if it is interfering.
This is my test for store function
/** #test */
public function storeTest()
{
$this->be($this->user);
$response = $this->json('POST', '/client/ocp/profile/247/route-', [
'name' => 'Test store',
'speed' => 4.5,
'created_by' => $this->user->id,
'client_id' => '262',
]);
$response
->seeStatusCode(302)
->seeJson(['status' => 'OK']);
}
when i run it i got this error
Invalid JSON was returned from the route. Perhaps an exception was thrown?
i try to add this header to solve a problem
['X-Requested-With' => 'XMLHttpRequest']
but it not solving
how i can solve this?
It always errors out saying one of 2 messages.
{"message":"Required field 'specList' is not specified"} or if I add specList then it says '400 Bad Request' response: {"message":"Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token"}
This is what I have:
$client = new GuzzleHttp\Client(['base_uri' => 'https://someURL/PrismGateway/services/rest/v1/']);
$res = $client->request('POST', 'vms/'.$vmId.'/clone',
[
'verify' => false,
'auth' => ['user', 'pass'],
'json' => [
'specList' => '[{"name":"test9"}]'
//tried 'create.dto.acropolis.VMCloneDTO' => '{"specList":[{"name":"test9"}]}'
]
]
);
If I use create.dto.acropolis.VMCloneDTO it tells {"message":"Required field 'specList' is not specified"} and if I change it up to specList it gives me the deserialize error from above. I'm not sure what I'm doing wrong but the actual param in the api is listed as create.dto.acropolis.VMCreateDTO and it expects the specList response as a json data type.
Someone help me figure out why it's not working?
Figured it out. Got rid of the json and added body:
'body' => '{"specList":[{"name":"test9"}]}'
I am using the HttpFoundation in my small project: use \Symfony\Component\HttpFoundation\JsonResponse as JsonResponse;
Unfortunately all my responses (tried JsonResponse, Response and BinaryFileResponse) only return a blank page, no errors and the code gets executed normally, e.g.
/* Get Inputs */
if (!$data = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL)) {
return new JsonResponse(array(
'result' => 'error',
'message' => 'URL is invalid or missing'
));
}else{
return new JsonResponse(array(
'result' => 'success',
'message' => 'FINE'
));
There are no errors in the logs either.
Any ideas how to approach the issue?
//UPDATE FOR CLARIFICATION
$json = new JsonResponse(array(
'result' => 'error',
'message' => 'Encrypt is invalid or missing'
));
echo $json;
returns HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: application/json {"result":"error","message":"Encrypt is invalid or missing"}
but why does the return not work?
You're not using the full stack framework, so you need to be sure that your front controller or equivalent calls $response->send(); to deliver the response to the client.
It's addition to answer:
$response = new JsonResponse(array(
'result' => 'error',
'message' => 'Encrypt is invalid or missing'
));
$response->send();