UNPKG

11.7 kBMarkdownView Raw
1## Bluecat <img src="https://raw.github.com/chenchaoyi/bluecat/master/images/bluecat.png" align="middle" />
2
3
4[![NPM version][npm-image]][npm-url]
5[![Build Status](https://travis-ci.org/chenchaoyi/bluecat.svg?branch=master)](https://travis-ci.org/chenchaoyi/bluecat)
6[![Dependency Status][david-image]][david-url]
7[![Downloads][downloads-image]][downloads-url]
8<!-- [![Gitter](https://badges.gitter.im/chenchaoyi/bluecat.svg)](https://gitter.im/chenchaoyi/bluecat?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)-->
9<!-- [![Gittip][gittip-image]][gittip-url] -->
10
11**Bluecat** is a library that helps to easily create HTTP requests and maintain session information underlayer.
12
13It could be integrated with any Node.js test framework and assertion library to create a clear and straighforward **Web services API testing framework**.
14
15* Define your APIs in a JSON file, **Bluecat** automatically creates all the methods for you
16* Callbacks are removed so complex requests flow is more clear
17* Full control over the HTTP request URL query, headers and body
18* Automatically maintains session cookies information for HTTP API call flows
19* [Convenience methods](#usage) that help to handle more complex scenario
20* The `bluecat` command line interface comes with a nice configuration utility that helps you to create your test framework in less than a minute.
21
22## Table of contents
23
24- [Installation](#installation)
25- [Examples](#example)
26 - [Regular RESTful API](#regular-restful-api)
27 - [Control query and/or headers in request](#control-query-andor-headers-in-request)
28 - [RESTful API with characters that cannot be used with dot notation in the URL](#restful-api-with-characters-that-cannot-be-used-with-dot-notation-in-the-url)
29 - [RESTful API with parameters in the URL](#restful-api-with-parameters-in-the-url)
30 - [Full example](#full-example-of-test-structure-using-bluecat)
31- [Usage](#usage)
32- [Command line tool](#command-line-tool)
33- [Logging](#logging)
34- [License](#license)
35
36---
37
38## Installation ##
39```bash
40$ npm install bluecat
41```
42
43---
44
45## Example ##
46
47#### Regular RESTful API
48```
49POST /checkout/contract
50GET /checkout/contract
51```
52
53* First define your API in config/api.json, following the exact URL path structure:
54
55```
56{
57 "api": {
58 "checkout": {
59 "contract": {
60 "schema": "http",
61 "method": ["GET", "POST"]
62 }
63 }
64 }
65}
66```
67
68* Then create a Bluecat service object. You are all set to send request and validate response:
69
70```javascript
71var expect = require('chai').expect;
72var Bluecat = require('bluecat');
73var Service = new Bluecat.ServiceSync(Bluecat.Api('api'), 'sample-host.com');
74
75// All requests need to be put as callback function in Service.run(), so they will run synchronously
76Service.run(function() {
77 // send POST http://sample-host.com/checkout/contract
78 // with body: {"cartid": "test-cart-id"}
79 var r = Service.checkout.contract.POST({
80 body: {
81 cartid: 'test-cart-id'
82 }
83 });
84 // verify response
85 expect(r.data.statusCode).to.equal(200);
86 expect(r.data.body).to.have.ownProperty('id');
87
88 // send GET http://sample-host.com/checkout/contract
89 // cookies are automatically maintained if there is any
90 r = Service.checkout.contract.GET();
91 // verify response
92 expect(r.data.statusCode).to.equal(200);
93 expect(r.data.body.cartId).to.eql('test-cart-id');
94})
95
96```
97
98#### Control query and/or headers in request
99```
100PUT /search/fitness/result?start=0&limit=50&error=true
101```
102
103* First define your API in config/api.json:
104
105```
106{
107 "api": {
108 "search": {
109 "fitness": {
110 "result": {
111 "schema": "https",
112 "method": ["PUT"]
113 }
114 }
115 }
116 }
117}
118```
119
120* Then create a Bluecat service object. You are all set to send request and validate response:
121
122```javascript
123var expect = require('chai').expect;
124var Bluecat = require('bluecat');
125var Service = new Bluecat.ServiceSync(Bluecat.Api('api'), 'sample-host.com');
126
127// All requests need to be put as callback function in Service.run(), so they will run synchronously
128Service.run(function() {
129 // send PUT http://sample-host.com/search/fitness/result?start=0&limit=50&error=true
130 // with body: {"term": "testTerm"}
131 // and header: {"User-agent": "automation"}
132 var r = Service.search.fitness.result.PUT({
133 body: {
134 term: 'testTerm'
135 },
136 query: {
137 start: 0,
138 limit: 50,
139 error: true
140 },
141 headers: {
142 'User-agent': 'automation'
143 }
144 });
145 // verify response
146 expect(r.data.statusCode).to.equal(200);
147})
148
149```
150
151
152#### RESTful API with characters that cannot be used with [dot notation] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) in the URL
153```
154GET /cart/v1/add-item/item
155```
156
157* First define your API in config/api.json:
158
159```
160{
161 "api": {
162 "cart": {
163 "v1": {
164 "add-item": {
165 "item": {
166 "schema": "http",
167 "method": ["GET"]
168 }
169 }
170 }
171 }
172 }
173}
174```
175
176* Then create a Bluecat service object and send request:
177
178```javascript
179var expect = require('chai').expect;
180var Bluecat = require('bluecat');
181var Service = new Bluecat.ServiceSync(Bluecat.Api('api'), 'sample-host.com');
182
183// All requests need to be put as callback function in Service.run(), so they will run synchronously
184Service.run(function() {
185 // send GET http://sample-host.com/cart/v1/add-item/item
186 // we cannot use 'r = Service.cart.v1.add-item.item.GET()' because 'add-item' cannot be used
187 // as dot notation property accessor, we need to use bracket notation in such case
188 r = Service.cart.v1['add-item'].item.GET();
189 // verify response
190 expect(r.data.statusCode).to.equal(200);
191})
192
193```
194
195#### RESTful API with parameters in the URL
196```
197GET /checkout/${uuid}/contract
198```
199* First define your API in config/api.json:
200
201```
202{
203 "api": {
204 "checkout": {
205 "${uuid}": {
206 "contract": {
207 "schema": "http",
208 "method": ["GET"]
209 }
210 }
211 }
212 }
213}
214```
215
216* Then create a Bluecat service object. You are all set to send request and validate response:
217
218```javascript
219var expect = require('chai').expect;
220var Bluecat = require('bluecat');
221var Service = new Bluecat.ServiceSync(Bluecat.Api('api'), 'sample-host.com');
222
223// All requests are needed to be put as callback function in Service.run(), so they will run synchronously
224Service.run(function() {
225 // send GET http://sample-host.com/checkout/5e586387-6d5a-4874-8a98-5836bdc45c7b/contract
226 var r = Service.checkout['${uuid}'].contract.GET({
227 params: {
228 uuid: '5e586387-6d5a-4874-8a98-5836bdc45c7b'
229 }
230 });
231 // verify response
232 expect(r.data.statusCode).to.equal(200);
233})
234```
235
236#### Full example of test structure using Bluecat
237
238[Example](https://github.com/chenchaoyi/bluecat/tree/master/examples)
239
240
241---
242
243## Usage ##
244<!--Usage is a two steps process. First, define the API structure in config/api.json:-->
245
246#### `Bluecat.ServiceSync(api, host, options)`
247Create a new bluecat service object, with desired [options](https://github.com/request/request/blob/master/README.md#requestoptions-callback).
248```javascript
249var Bluecat = require('bluecat');
250var Api = Bluecat.Api('mobileapi');
251var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com', {
252 gzip: true
253});
254```
255
256#### `rawRequest(options)`
257Sometimes we just want to send a request to some host, which is different than the API host we gave to the bluecat service object. You can use `rawRequest(options)` to send it.
258
259```javascript
260var Bluecat = require('bluecat');
261var Api = Bluecat.Api('mobileapi');
262var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
263
264var r = Service.rawRequest({
265 method: 'GET',
266 json: true,
267 uri: 'https://thirdparty-host/creditcard/encryption.js',
268 headers: {'accept-encoding': 'gzip'},
269});
270expect(r.err).to.equal(null);
271expect(r.data.statusCode).to.equal(200);
272```
273
274#### `setProxy(proxy)`
275Set proxy address, all the requests will be sent via a connection to the proxy server.
276```javascript
277var Bluecat = require('bluecat');
278var Api = Bluecat.Api('mobileapi');
279var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
280
281Service.setProxy('http://127.0.0.1:8888')
282```
283
284#### `resetCookie()`
285Clean up cookie jar, so the next request won't set any cookies in the header.
286
287```javascript
288var Bluecat = require('bluecat');
289var Api = Bluecat.Api('mobileapi');
290var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
291
292Service.v1.products.search.GET();
293Service.resetCookie();
294Service.v1.cart.POST({
295 body: {
296 location: '94066'
297 }
298})
299```
300
301#### `setHeaders(headers)`
302Set headers that will be sent in all the requests.
303
304```javascript
305var Bluecat = require('bluecat');
306var Api = Bluecat.Api('mobileapi');
307var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
308
309Service.setHeaders({'User-Agent': 'Automation'});
310```
311
312#### `setSessionRules(rules)`
313Set extra session rules other than cookie. Some RESTful APIs defines their own session rules, you can set such rules to the bluecat service object, so you don't have to deal with it before sending every single HTTP request.
314
315```javascript
316var Bluecat = require('bluecat');
317var Api = Bluecat.Api('mobileapi');
318var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
319
320// The following sessions rules start with value 'start-auth-token-value' in the request header AUTH_TOKEN,
321// then grab new value from response header REFRESH_AUTH_TOKEN
322// and put it in the next request header AUTH_TOKEN
323Service.setSessionRules({
324 requestHeader: 'AUTH_TOKEN',
325 responseHeader: 'REFRESH_AUTH_TOKEN',
326 startSessionHeader: 'start-auth-token-value'
327});
328```
329
330#### `sleep(ms)`
331Sleep for `ms` milliseconds.
332
333```javascript
334var Bluecat = require('bluecat');
335var Api = Bluecat.Api('mobileapi');
336var Service = new Bluecat.ServiceSync(Api, 'api.mobile.walmart.com');
337// Sleep for 5 seconds
338Service.sleep(5000);
339```
340
341
342---
343
344## Command line tool ##
345Bluecat comes with `bluecat` command line interface that helps you to create a basic Web services API test framework and check defined APIs.
346
347#### Create basic Web services API test structure:
348
349```bash
350$ npm install bluecat
351$ ./node_modules/.bin/bluecat config
352```
353Follow the instructions and it will create a scaffold of a basic web services API test framework for you.
354
355#### Check currently defined Web services APIs:
356
357```bash
358$ ./node_modules/.bin/bluecat api
359```
360It will print out all the current defined Web services APIs that could be called from test case.
361
362---
363
364## Logging
365
366* 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.
367
368* Launch the node process like `BLUECAT_DEBUG_CONSOLE=true node script.js` to see all the requests/responses information from your console (stdout).
369
370---
371
372## License
373Licensed under the [MIT](http://opensource.org/licenses/MIT)
374
375[npm-image]: https://img.shields.io/npm/v/bluecat.svg?style=flat-square
376[npm-url]: https://www.npmjs.org/package/bluecat
377[github-tag]: http://img.shields.io/github/tag/chenchaoyi/bluecat.svg?style=flat-square
378[github-url]: https://github.com/chenchaoyi/bluecat/tags
379[david-image]: http://img.shields.io/david/chenchaoyi/bluecat.svg?style=flat-square
380[david-url]: https://david-dm.org/chenchaoyi/bluecat
381[license-image]: http://img.shields.io/npm/l/bluecat.svg?style=flat-square
382[license-url]: http://opensource.org/licenses/MIT
383[downloads-image]: http://img.shields.io/npm/dm/bluecat.svg?style=flat-square
384[downloads-url]: https://npmjs.org/package/bluecat
385[gittip-image]: https://img.shields.io/gittip/chenchaoyi.svg?style=flat-square
386[gittip-url]: https://www.gittip.com/chenchaoyi/
387