vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
http://frisbyjs.com
1.53k stars 201 forks source link

POST request frisby #60

Closed moses-gangipogu closed 10 years ago

moses-gangipogu commented 10 years ago

/* * Tests -> POST add a new user / frisby.create('testAddUser') .post('http://localhost/payCard/Api/User', JSON.stringify({ userName : 'newApi', Password : '123456', installationId : 1, cityId : 2, }),{ json: true },{METHOD: 'POST'}) .inspectJSON() .inspectBody()
.expectStatus(200) .expectHeaderContains('content-type', 'application/json') .expectJSON('status',{ StatusMSG: "SUCCESS", Code: '1' })
.toss();

console.log(frisby.create('testAddUser')); console.log(JSON.stringify({ userName : 'newApi', Password : '123456', installationId : 1, cityId : 2, })); // is this code right and how can i check what and how the json data is going to the api

//api code

//output is INVALID JSON

vlucas commented 10 years ago

Your Frisby test should look like this:

frisby.create('testAddUser')
.post('http://root:123456@localhost/payCard/Api/User', {
    userName : 'newApi',
    Password : '123456',
    installationId : 1,
    cityId : 2,
}), { json: true })
.inspectJSON()
.inspectBody()
.toss()

As for your PHP code, it's best to build an array and then run the whole array though json_encode, like this:

$jsonData = array();

// blah blah
$jsonData['status'] = array('StatusMSG' => 'Invalid JSON', 'Code' => 20);

// something else
$jsonData['foo'] = 'bar';

// then ONCE at the end of the script
echo json_encode($jsonData);
moses-gangipogu commented 10 years ago

thank you sir it has helped me alot

moses-gangipogu commented 10 years ago

hello sir try this its giving me the 406 HTTP error status

/******this delete rquest works perfectly / * Tests -> DELETE delete an existing user based on user User_Id / frisby.create('testDeleteUser') .delete('http://localhost/payCard/Api/User/22') .expectStatus(200) .expectHeaderContains('content-type', 'application/json') .expectJSON('status',{ StatusMSG: "SUCCESS", Code: '1' })
.toss();

/**but the request with json data into it is not working and giving 406 error

/**
* Tests -> POST add a new user
*/
frisby.create('testAddUser')
.post('http://localhost/payCard/Api/User', {
userName : 'newApi',
Password : '123456',
installationId : 1,
cityId : 2,
}, { json: true })
.inspectJSON()
.inspectBody()
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.expectJSON('status',{
StatusMSG: "SUCCESS",
Code: '1'

})
.toss();

/***output

2) Frisby Test: testAddUser [ POST http://localhost/payCard/Api/User ] Message: Expected 406 to equal 200. Stacktrace: Error: Expected 406 to equal 200. at null. (/var/www/frisby/lib/frisby.js:449:42) at null. (/var/www/frisby/lib/frisby.js:1026:43) at Timer.listOnTimeout as ontimeout

3) Frisby Test: testAddUser [ POST http://localhost/payCard/Api/User ] Message: Expected 'text/html; charset=iso-8859-1' to contain 'application/json'. Stacktrace: Error: Expected 'text/html; charset=iso-8859-1' to contain 'application/json'. at null. (/var/www/frisby/lib/frisby.js:474:67) at null. (/var/www/frisby/lib/frisby.js:1026:43) at Timer.listOnTimeout as ontimeout

4) Frisby Test: testAddUser [ POST http://localhost/payCard/Api/User ] Message: Error: Error parsing JSON string: Unexpected '<' Given: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

406 Not Acceptable

Not Acceptable

An appropriate representation of the requested resource /payCard/Api/User could not be found on this server.

Available variants:
Apache/2.2.22 (Ubuntu) Server at localhost Port 80

Stacktrace: Error: Error parsing JSON string: Unexpected '<' Given: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

406 Not Acceptable

Not Acceptable

An appropriate representation of the requested resource /payCard/Api/User could not be found on this server.

Available variants:
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
at _jsonParse (/var/www/frisby/lib/frisby.js:1150:11)
at null.<anonymous> (/var/www/frisby/lib/frisby.js:606:20)
at null.<anonymous> (/var/www/frisby/lib/frisby.js:1026:43)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

5) Frisby Test: testAddUser [ POST http://localhost/payCard/Api/User ] Message: Error: Error parsing JSON string: Unexpected '<' Given: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

406 Not Acceptable

Not Acceptable

An appropriate representation of the requested resource /payCard/Api/User could not be found on this server.

Available variants:
Apache/2.2.22 (Ubuntu) Server at localhost Port 80

Stacktrace: Error: Error parsing JSON string: Unexpected '<' Given: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

406 Not Acceptable

Not Acceptable

An appropriate representation of the requested resource /payCard/Api/User could not be found on this server.

Available variants:
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
at _jsonParse (/var/www/frisby/lib/frisby.js:1150:11)
at Frisby.<anonymous> (/var/www/frisby/lib/frisby.js:862:30)
at Frisby.<anonymous> (/var/www/frisby/lib/frisby.js:892:8)
at null.<anonymous> (/var/www/frisby/lib/frisby.js:1057:18)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Finished in 0.141 seconds 5 tests, 19 assertions, 5 failures, 0 skipped

THANK YOU

moses-gangipogu commented 10 years ago

is there any thing in this line below

frisby.globalSetup({ request: { / //adding this line to the code also give me an 406 HTTP error even in delete request headers:{'Accept': 'application/json'} },/ timeout: (30 * 1000) });

moses-gangipogu commented 10 years ago

request: { headers:{'Accept': 'application/json, application/x-httpd-php'} },

its run perfect just by adding this line in the globalSetup can u please explain why this problem came and how its solved??????????????

moses-gangipogu commented 10 years ago

thank you and happy new year

moses-gangipogu commented 10 years ago

OS = Ubuntu 12.04 LTS i am using .. is this because of this , i tried this same code in windows 7 it runs successfully in it without /**

headers:{'Accept': 'application/json, application/x-httpd-php'}

**/

this line ..but in Ubuntu 12.04 LTS .. its giving 406 error as given in my previous comment without this line's

.. thank you and sorry for my mistakes in English

vlucas commented 10 years ago

HTTP 406 means the server can't accept or process the format that you're sending. Try sending the POST request without {json: true} so it gets transmitted as form-data:

frisby.create('testAddUser')
.post('http://root:123456@localhost/payCard/Api/User', {
    userName : 'newApi',
    Password : '123456',
    installationId : 1,
    cityId : 2,
})
.inspectJSON()
.inspectBody()
.toss()
moses-gangipogu commented 10 years ago

thanks

mrded commented 8 years ago

Why { json: true } argument hasn't been mentioned in documentation?

maneeshrao66 commented 7 years ago

Hello I am facing 500 error during delete request into frisby. The code is below please correct me if I have done any wrong..!

var frisby = require('frisby');

//get the current UTC time from this URL: (https://currentmillis.com/) var now = new Date(); var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()) var currentUTCTime = now_utc.getTime() - (now_utc.getTimezoneOffset() * 60000);

frisby.create('Authentication for Delete Request Valid User Name and Password')

     .delete('http://example.com',
      {
       headers:
        {
              'Accept':'application/json',
              'Auth-Username':'abcd',
              'X-Application-Key':'xyzzzz',
              'Content-Type':'application/json',
              'x-locale':'en_US',
              'x-microtime':currentUTCTime, //get the current UTC time
              'auth-signature': 'example', // Added new auth-signature for new responses
              }
           }   
        )
  .expectStatus(200)
  .toss()