Skip to content

Commit

Permalink
add mocha style unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 25, 2012
1 parent fa3e283 commit 3b16e82
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 80 deletions.
16 changes: 2 additions & 14 deletions Makefile
Expand Up @@ -4,22 +4,10 @@ REPORTER = spec
MOCHAOPTS= MOCHAOPTS=


build: build:
@node-gyp clean && node-gyp configure && node-gyp build @node-gyp clean configure build


test: test:
@NODE_ENV=test ./node_modules/mocha/bin/mocha \ @NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) --timeout $(TIMEOUT) $(MOCHAOPTS) $(TESTS) --reporter $(REPORTER) --timeout $(TIMEOUT) $(MOCHAOPTS) $(TESTS)


test-cov: lib-cov .PHONY: build test
@HTTPSYNC_COV=1 $(MAKE) test
@HTTPSYNC_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html

lib-cov:
@rm -rf lib-cov
@jscoverage lib lib-cov

clean:
@rm -rf lib-cov
@rm -f coverage.html

.PHONY: build test test-cov lib-cov clean
34 changes: 24 additions & 10 deletions README.md
@@ -1,19 +1,17 @@
# node-curl # httpsync


node-curl is a port of libcurl to node.js. Its interface emulates the [![Build Status](https://secure.travis-ci.org/fengmk2/node-curl.png)](http://travis-ci.org/fengmk2/node-curl)
`http` module of node.js. But in contrast to `http` module's asynchronous
functions, node-curl provides the equivalent synchronous APIs. `httpsync` is a port of libcurl to node.js. Its interface emulates the
`http` module of node.js. But in contrast to `http` module's asynchronous
functions, node-curl provides the equivalent synchronous APIs.


## Install ## Install


```bash ```bash
$ npm install httpsync $ npm install httpsync
``` ```


## Build

node-waf configure && node-waf build

## APIs ## APIs


### curl.request(options) ### curl.request(options)
Expand Down Expand Up @@ -78,7 +76,7 @@ req.write("another text");
console.log(req.end()); console.log(req.end());
``` ```


### request.endFile (filePath) ### request.endFile(filePath)


Send a file directly. The method will default to `PUT`. Send a file directly. The method will default to `PUT`.


Expand All @@ -101,6 +99,22 @@ req.endFile("/etc/passwd");
- `ip` IP address of the server. - `ip` IP address of the server.
- `statusCode` Status code that sent by server. - `statusCode` Status code that sent by server.


## Contributors

Thanks goes to the people who have contributed code to this module, see the [GitHub Contributors page](https://github.com/fengmk2/node-curl/graphs/contributors).

Below is the output from `git-summary`

```
project: node-curl
commits: 25
active : 5 days
files : 21
authors:
21 赵成 84.0%
4 fengmk2 16.0%
```

## License ## License


(The MIT License) (The MIT License)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -2,7 +2,8 @@
"name": "httpsync", "name": "httpsync",
"version": "0.0.1", "version": "0.0.1",
"scripts": { "scripts": {
"install": "node-gyp clean && node-gyp configure && node-gyp build" "install": "node-gyp clean configure build",
"test": "make test"
}, },
"description": "node-curl is a port of libcurl to node.js. Its interface emulates the `http` module of node.js. But in contrast to `http` module's asynchronous functions, node-curl provides the equivalent synchronous APIs.", "description": "node-curl is a port of libcurl to node.js. Its interface emulates the `http` module of node.js. But in contrast to `http` module's asynchronous functions, node-curl provides the equivalent synchronous APIs.",
"main": "index.js", "main": "index.js",
Expand All @@ -11,7 +12,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/zcbenz/node-curl.git" "url": "git://github.com/fengmk2/node-curl.git"
}, },
"keywords": [ "keywords": [
"curl", "curl",
Expand Down
225 changes: 216 additions & 9 deletions test/curl.js
Expand Up @@ -12,18 +12,24 @@


var httpsync = require('../'); var httpsync = require('../');
var should = require('should'); var should = require('should');
var echo = require('./support/echo'); var spawn = require('child_process').spawn;


describe('curl.js', function () { describe('curl.js', function () {


var app = echo.create();
var homeurl = 'http://127.0.0.1:'; var homeurl = 'http://127.0.0.1:';
var echoProcess;
before(function (done) { before(function (done) {
app.listen(0, function () { var echofile = __dirname + '/support/echo.js';
homeurl += app.address().port; echoProcess = spawn('node', [echofile]);
echoProcess.stdout.once('data', function (data) {
data = JSON.parse(data);
homeurl += data.port;
done(); done();
}); });
}); });
after(function () {
echoProcess.kill();
});


describe('request()', function () { describe('request()', function () {
it('should throw error', function () { it('should throw error', function () {
Expand Down Expand Up @@ -51,11 +57,201 @@ describe('curl.js', function () {
}).should.throw('Couldn\'t resolve host name'); }).should.throw('Couldn\'t resolve host name');
}); });


// it('should GET / return status 200', function () { it('should GET / return status 200', function () {
// var req = httpsync.get(homeurl); var req = httpsync.request({
// var res = req.end(); url: homeurl
// res.should.status(200); });
// }); var res = req.end();
res.should.header('x-request-url', '/');
res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.should.be.instanceof(Buffer);
res.data.toString().should.equal('GET\n');
});

it('should ignore data when GET with some data', function () {
var req = httpsync.request({
url: homeurl,
method: 'GET'
});
var res = req.end('some data');
res.should.header('x-request-url', '/');
res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.toString().should.equal('GET\n');
});
it('should POST with empty data', function () {
var req = httpsync.request({
url: homeurl + '/post',
method: 'post'
});
var res = req.end();
res.should.header('x-request-url', '/post');
res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.toString().should.equal('POST\n');
});
it('should return data when POST with some data', function () {
var req = httpsync.request({
url: homeurl + '/post',
method: 'post'
});
req.write ("1 line\n");
req.write ("2 line\n");
req.write ("3 line\n");
var res = req.end ("helloworld");
res.data.toString().should.equal("POST\n1 line\n2 line\n3 line\nhelloworld");
res.should.header('x-request-url', '/post');
res.should.header('content-type', 'text/plain');
res.should.status(200);
});
it('should POST with many data', function () {
var req = httpsync.request({
url: homeurl + '/pOst',
method: 'pOst'
});
var res = req.end('some data');
res.should.header('x-request-url', '/pOst');
res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.toString().should.equal('POST\nsome data');
});

it('should HEAD return empty body', function () {
var req = httpsync.request({
url: homeurl + '/HEAD',
method: 'HeAD'
});
var res = req.end('some data');
res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.toString().should.equal('');
});

it('should request with other methods', function () {
var req = httpsync.request({
url: homeurl + '/put',
method: 'PUT'
});
var res = req.end('put data');
res.should.header('x-request-url', '/put');
// res.should.header('content-type', 'text/plain');
res.should.status(200);
res.data.toString().should.equal('PUT\nput data');

var req2 = httpsync.request({
url: homeurl + '/delete',
method: 'DELETE'
});
var res2 = req2.end('delete DATA');
res2.should.header('x-request-url', '/delete');
res2.should.header('content-type', 'text/plain');
res2.should.status(200);
res2.data.toString().should.equal('DELETE\n');
});

it('should request with custom headers', function () {
var req1 = httpsync.request({
url: homeurl,
headers: {
"Custom": "true",
"String": "A very very long string",
"How": "Old are you"
}
});
var res1 = req1.end ();

var req2 = httpsync.request({
url: homeurl,
method: "DELETE",
headers: {
"custom": "true",
"File": "/etc/passwd",
"Directory": "/usr"
}
});
var res2 = req2.end();
var host = homeurl.replace('http://', '');
res1.data.toString().should.equal("GET\n{\"user-agent\":\"zcbenz/node-curl\",\"host\":\"" +
host + "\",\"accept\":\"*/*\",\"custom\":\"true\",\"string\":\"A very very long string\",\"how\":\"Old are you\"}");
res1.should.status(200);
res2.data.toString().should.equal("DELETE\n{\"user-agent\":\"zcbenz/node-curl\",\"host\":\"" +
host + "\",\"accept\":\"*/*\",\"custom\":\"true\",\"file\":\"/etc/passwd\",\"directory\":\"/usr\"}");
res2.should.status(200);
});

it('should write with buffer', function () {
var req = httpsync.request({
url: homeurl,
method: "POST",
headers: { "nomethod": "true" }
});
var buf = new Buffer (8);
buf.writeUInt8(0x3, 0, 'big');
buf.writeUInt8(0x4, 1, 'big');
buf.writeUInt8(0x23, 2, 'big');
buf.writeUInt8(0x42, 3, 'big');
buf.writeUInt8(0x3, 4, 'little');
buf.writeUInt8(0x4, 5, 'little');
buf.writeUInt8(0x23, 6, 'little');
buf.writeUInt8(0x42, 7, 'little');
var res = req.end(buf);
res.data.should.eql(buf);
});

it('should endFile() to send file data', function () {
var req = httpsync.request({
url: homeurl
});
var res = req.endFile(__filename);

var req2 = httpsync.request({
url: homeurl,
method: "POST"
});
var res2 = req2.endFile(__filename);
res.data.toString("utf8").should.equal("PUT\n" + require("fs").readFileSync(__filename));
res2.data.toString("utf8").should.equal("POST\n" + require("fs").readFileSync(__filename));
});

it('should check responded headers', function () {
var req = httpsync.request ({
url: homeurl,
headers: {
"customheaders": "Test String"
}
});
var res = req.end();
res.should.header('customheaders', 'Test String');
res.should.status(42);
});

it('should throw err when request with invalid options', function () {
(function () {
var req = httpsync.request ({
url: homeurl,
headers: {
"customheaders": "Test String",
},
method: 'FUCK'
});
var res = req.end();
}).should.throw('Server returned nothing (no headers, no data)');
});

it('should throw timeout', function () {
(function () {
var req = httpsync.request ({
url: homeurl,
timeout: 1,
headers: {
"customheaders": "Test String",
timeout: 'true'
},
});
var res = req.end();
}).should.throw('Timeout was reached');
});
}); });


describe('get()', function () { describe('get()', function () {
Expand Down Expand Up @@ -93,6 +289,17 @@ describe('curl.js', function () {
var res = req.end(); var res = req.end();
res.should.status(200); res.should.status(200);
}); });

it('should GET with options', function () {
var req = httpsync.get({
url: homeurl + '/get'
});
var res = req.end();
res.should.status(200);
res.should.header('x-request-url', '/get');
res.should.header('x-request-method', 'GET');
res.data.toString().should.equal('GET\n');
});
}); });


}); });

0 comments on commit 3b16e82

Please sign in to comment.