2

I have used Frisby framework to write the API test cases. when I put the url and credential into postman or DHC, the responses are coming correctly but in the frisby test cases I am getting error:-

The code is below:-

var frisby = require('frisby');
frisby.create('User Authentication')

//send the params to below url
   .post("url",
      { username: "username",
        password: "password"
      },
      { json: true,
        headers: {Accept:'application/json',
                 'Accept-Encoding':'Encoding:gzip, deflate',
                 'Accept-Language':'en-US,en;q=0.8',
                 Connection:'keep-alive',
                 'Content-Length':'107',
                 'Content-Type':'application/json',
                 Host:'url.com',
                 Origin:'http://url.com',
                 Referer:'http://url.com/',
                 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
                 'X-Application-Key':'1234545',
                 'X-Requested-With':'XMLHttpRequest'
                 }
       }
    )

.expectStatus(200)
.toss(); 
Ray
  • 40,256
  • 21
  • 101
  • 138

1 Answers1

0

The request is coming back as a 400 which is a HTTP Bad Request.

First thing I'd do vomit out the response message body the API sent back to you to see any details of what is wrong. Bad Request responses tend to tell you what's wrong with the request.

Since frisby is asyncronous, you ensure order of execution by using after() (before you toss).

.post("https://blah.blah.blah.com", "some json...", "some more json...")
.expectStatus(200)
.after(function(err, res, body){ console.log(body); })
.toss(); 

Now, frisby's post() takes 3 arguments:

  • url : the url
  • data : the body of your post request
  • params : parameters for the frisby post function post like telling it to submit json.

So it looks you're posting (maybe creating) a username/password object.

I'm assuming the string "url" is really some valid url, so we can skip that obvious issue.

Taking a wild shot without seeing any error message, I'm going to guess your issue is you're not providing auth credentials to the API. I don't know of many API totally open. Typically, you expect something like Basic Auth credentials and if you don't provide them, you'll typically get a:

  • 400 bad request if they're nice with a message saying "no credentials"
  • 404 not found if they're playing coy and don't want non authorized folks to know there's a valid resource there
  • or maybe even a 403 Forbidden

If it does turn out to be credentials, you add them after you create and call post, but before you toss(). If you put it before post() it will fail. Also, this is the API username & password, not any user you're trying to create in the system. Sometimes they're referred to in API's a client_id and client_secret or API_KEY and API_SECRET, but it's just an identifier and a shared secret.

So the final thing would look like

.post("https://blah.blah.blah.com", "some json...", "some more json...")

.auth("api_username", "api_password")

.expectStatus(200)

.toss(); 

Frisby will convert the plaintext username/password to a valid base 64 encode string and stick it in the Authorization: header (probably why it needs to happen after calling post())

Also, I like explicitly adding headers using the addHeader(header, content) over dropping a big nasty blob of json in the post(), but that's just my preference.

Good Luck!

Ray
  • 40,256
  • 21
  • 101
  • 138