Skip to content

Commit

Permalink
Merge pull request #70 from BigstickCarpet/browser-support
Browse files Browse the repository at this point in the history
Browser support
  • Loading branch information
keithamus committed Feb 1, 2016
2 parents 6a5000c + b3fbcc1 commit 3db5545
Show file tree
Hide file tree
Showing 12 changed files with 5,334 additions and 138 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
TESTS = test/*.js
REPORTER = spec

all:
all: bundle
@rm -f README.md
@node ./support/readme.js

bundle:
@rm -rf dist
@mkdir dist
@browserify lib/http.js --outfile dist/chai-http.js --standalone chaiHttp

test:
@NODE_ENV=test ./node_modules/.bin/mocha \
--require ./test/bootstrap \
Expand All @@ -22,4 +27,4 @@ clean:
@rm -rf lib-cov
@rm -f coverage.html

.PHONY: all test lib-cov test-cov clean
.PHONY: all bundle test lib-cov test-cov clean
54 changes: 36 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ var chai = require('chai')
chai.use(chaiHttp);
```

To use Chai HTTP on a web page, just include the [`dist/chai-http.js`](dist/chai-http.js) file:

```html
<script src="chai.js"></script>
<script src="chai-http.js"></script>
<script>
chai.use(chaiHttp);
</script>
```

## Integration Testing

Expand All @@ -45,6 +54,8 @@ or a node.js http(s) server as the foundation for your request.
If the server is not running, chai-http will find a suitable
port to listen on for a given test.

__Note:__ This feature is only supported on Node.js, not in web browsers.

```js
chai.request(app)
.get('/')
Expand Down Expand Up @@ -133,7 +144,12 @@ chai.request(app)
})
```

Since Node.js version 0.10.x and lower does not have native promise support you can use [kriskowal/q](https://github.com/kriskowal/q) for them. Use the `addPromise` function to do this. For example:
__Note:__ Node.js version 0.10.x and some older web browsers do not have
native promise support. You can use any promise library, such as
[es6-promise](https://github.com/jakearchibald/es6-promise) or
[kriskowal/q](https://github.com/kriskowal/q) and call the `addPromise`
method to use that library with Chai HTTP. For example:

```js
var chai = require('chai');
chai.use(require('chai-http'));
Expand Down Expand Up @@ -169,9 +185,9 @@ agent

### .then (resolveCb, rejectCb)

* **@param** _{Function}_ resolveCB
* **@param** _{Function}_ resolveCB
* **@cb** {Response}
* **@param** _{Function}_ rejectCB
* **@param** _{Function}_ rejectCB
* **@cb** {Error}

Invoke the request to to the server. The response
Expand All @@ -188,10 +204,9 @@ chai.request(app)
});
```


### .catch (rejectCb)

* **@param** _{Function}_ rejectCB
* **@param** _{Function}_ rejectCB
* **@cb** {Error}

Invoke the request to to the server, catching any
Expand All @@ -206,7 +221,6 @@ chai.request(app)
});
```


## Assertions

The Chai HTTP module provides a number of assertions
Expand All @@ -222,32 +236,42 @@ Assert that a response has a supplied status.
expect(res).to.have.status(200);
```


### .header (key[, value])

* **@param** _{String}_ header key (case insensitive)
* **@param** _{String|RegExp}_ header value (optional)

Assert that an object has a header. If a value is
provided, equality to value will be asserted. You may also pass a regular expression to check.
Assert that a `Response` or `Request` object has a header.
If a value is provided, equality to value will be asserted.
You may also pass a regular expression to check.

__Note:__ When running in a web browser, the
[same-origin policy](https://tools.ietf.org/html/rfc6454#section-3)
only allows Chai HTTP to read
[certain headers](https://www.w3.org/TR/cors/#simple-response-header),
which can cause assertions to fail.

```js
expect(req).to.have.header('x-api-key');
expect(req).to.have.header('content-type', 'text/plain');
expect(req).to.have.header('content-type', /^text/);
```


### .headers


Assert that an object has headers.
Assert that a `Response` or `Request` object has headers.

__Note:__ When running in a web browser, the
[same-origin policy](https://tools.ietf.org/html/rfc6454#section-3)
only allows Chai HTTP to read
[certain headers](https://www.w3.org/TR/cors/#simple-response-header),
which can cause assertions to fail.

```js
expect(req).to.have.headers;
```


### .ip


Expand All @@ -258,7 +282,6 @@ expect('127.0.0.1').to.be.an.ip;
expect('2001:0db8:85a3:0000:0000:8a2e:0370:7334').to.be.an.ip;
```


### .json / .text / .html


Expand All @@ -270,7 +293,6 @@ expect(req).to.be.html;
expect(req).to.be.text;
```


### .redirect


Expand All @@ -280,7 +302,6 @@ Assert that a `Response` object has a redirect status code.
expect(res).to.redirect;
```


### .redirectTo

* **@param** _{String}_ location url
Expand All @@ -291,7 +312,6 @@ Assert that a `Response` object redirects to the supplied location.
expect(res).to.redirectTo('http://example.com');
```


### .param

* **@param** _{String}_ parameter name
Expand All @@ -306,7 +326,6 @@ expect(req).to.have.param('orderby', 'date');
expect(req).to.not.have.param('limit');
```


### .cookie

* **@param** _{String}_ parameter name
Expand All @@ -324,7 +343,6 @@ expect(res).to.have.cookie('session_id', '1234');
expect(res).to.not.have.cookie('PHPSESSID');
```


## License

(The MIT License)
Expand Down
Loading

0 comments on commit 3db5545

Please sign in to comment.