Giter Club home page Giter Club logo

bluecat's Introduction

Bluecat

NPM version Build Status Dependency Status Downloads

A REST API testing framework built on Node.js that makes testing API endpoints straightforward.

  • Define your APIs in a json file, Bluecat will create all the methods for you
  • Callbacks are removed so tests that have a complex API call flow will be more clear
  • Full control over the request URL query, headers and body in test case
  • Automatically maintains session cookies for you for HTTP API call flows
  • Convenience methods that helps to handle complex scenario

Table of contents


Installation

$ npm install bluecat

Example

Regular RESTful API

POST /checkout/contract
GET  /checkout/contract
  • First define your API in config/api.json:
{
  "api": {
    "checkout": {
      "schema": "http",
      "method": ["GET", "POST"]
      }
  }
}
  • Then create a Bluecat service object. You are all set to send request and validate response:
var expect = require('chai').expect;
var Bluecat = require('bluecat');
var Service = new Bluecat.ServiceSync(Bluecat.Api('api'), 'sample-host.com');

// All requests need to be put into Api.run(), so they will run synchronously
Service.run(function() {
    // send POST http://sample-host.com/checkout/contract
    var r = Service.checkout.contract.POST({
      body: {
        cartid: 'test-cart-id'
      }
    });
    // verify response
    expect(r.data.statusCode).to.equal(200);
    expect(r.data.body).to.have.ownProperty('id');

    // send GET http://sample-host.com/checkout/contract
    // cookies are automatically maintained
    r = Service.checkout.contract.GET();
    // verify response
    expect(r.data.statusCode).to.equal(200);
    expect(r.data.body.cartId).to.eql('test-cart-id');
})

Usage

Bluecat.ServiceSync(api, host, options)

Create a new bluecat service object, with desired options.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com', {
  gzip: true
});

rawRequest(options)

Sometimes we just want to send a request to some host, which is different than the API host we are testing. You can use rawRequest(options) to fully to send it.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');

var r = Service.rawRequest({
  method: 'GET',
  json: true,
  uri: 'https://thirdparty-host/creditcard/encryption.js',
  headers: {'accept-encoding': 'gzip'},
});
expect(r.err).to.equal(null);
expect(r.data.statusCode).to.equal(200);

setProxy(proxy)

Set proxy address, all the requests will be sent via a connection to the proxy server.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');

Service.setProxy('http://127.0.0.1:8888')

resetCookie()

Clean up cookie jar, so the next request won't set any cookies in the header.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');

Service.v1.products.search.GET();
Service.resetCookie();
Service.v1.cart.POST({
  body: {
    location: '94066'
  }
})

setHeaders(headers)

Set headers that will be set in all the requests.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');

Service.setHeaders({'User-Agent': 'Automation'});

setSessionRules(rules)

Set extra session rules other than cookie. Some RESTful APIs defines their own session rules, you can set it in the Bluecat framework so you don't have to deal with it in the actual test case.

var Bluecat = require('bluecat');
var Api = Bluecat.Api('mobileapi');
var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');

// The following sessions rules start with 'start-auth-token-value' in the request header AUTH_TOKEN,
// then grab new value from response header REFRESH_AUTH_TOKEN
// and put it in the next request header AUTH_TOKEN
Service.setSessionRules({
  requestHeader: 'AUTH_TOKEN',
  responseHeader: 'REFRESH_AUTH_TOKEN',
  startSessionHeader: 'start-auth-token-value'
});

Logging

  • Launch the node process like BLUECAT_DEBUG_FILE=/path/to/bluecat.log node script.js to keep a log file of all the requests/responses information.

  • Launch the node process like BLUECAT_DEBUG_CONSOLE=true node script.js to see all the requests/responses information from your console (stdout).


License

Licensed under the MIT

bluecat's People

Contributors

chenchaoyi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.