javascriptrestautomated-testsweb-api-testingfrisby.js

Using a authentication login a parameter in request header in frisby


I'm trying to a test that requires an authentication token login using frisby, the problem is: the response of my request is a string and not a JSON and i still havent figured out how to get it working.

I've read a few examples that i found googling but they havent been helpful so far.

Here's my code:

var frisby= require('frisby');

frisby.create('Login')
    .post('http://mid.dev.psafe.com/My.API/security/Authentication.svc/Login',
        {"user":"115303640577606155760"},
        {json:true},
        { headers: { 'Content-Type': 'application/json' }})
    .expectStatus(200)
    .afterJSON(function (token) {
        frisby.globalSetup({
            request: {
                headers: { 'User-Token': token }
            }
        });

        frisby.create('List associated devices')
            .post('http://mid.dev.psafe.com/My.API/APIAdmin.svc/GetDevices',
                {json:true})
            .expectStatus(200)
        .toss();
    })

.toss();

Here's what i got when ran the test:

1) Frisby Test: Login 
    [ POST http://mid.dev.psafe.com/My.API/security/Authentication.svc/Login ]
   Message:
     Error: Error parsing JSON string: Unexpected token q
    Given: qO1DkNvKlCaOmNXsrmvGgfIqeCQEztKJHnizItWyfKaXD9H1bwm1eTIgiINCw5Dz0
   Stacktrace:
     Error: Error parsing JSON string: Unexpected token q
    Given: qO1DkNvKlCaOmNXsrmvGgfIqeCQEztKJHnizItWyfKaXD9H1bwm1eTIgiINCw5Dz0
    at _jsonParse (/usr/lib/node_modules/frisby/lib/frisby.js:1219:11)
    at Frisby.<anonymous>     (/usr/lib/node_modules/frisby/lib/frisby.js:950:20)
    at null.<anonymous> (/usr/lib/node_modules/frisby/lib/frisby.js:1112:18)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

Finished in 0.255 seconds
1 test, 2 assertions, 1 failure, 0 skipped

Thanks!


Solution

  • I fixed using .after() instead .afterJSON().