????????????e2e???????ж????????????????????ī?????????????????б??????????????????????????API?????????????????????????????????????°?????????e2e???????????????????????Ч??(????ο??????????)??
??????????????????API??????????????????API?????????????soapUI????postman???GUI?????????????У??????????????????????д??????ɡ???????????????????????????????ú??????????????????ɡ?
???????????
?????????????????е?API??????????к???????????????REST-assured??Rest-Assured ??????? Java ???? REST API ?????????????????????? REST API ??????????????д???????????????? HTTP ????????????????????????????
????Testing and validation of REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.
??????github????????????????????????????????????????????????????????????
??????????????????????????????????е???????tj?????????nodeJS??????supertest???????????????superagent??API??????????????????
????Super-agent driven library for testing node.js HTTP servers using a fluent API
????HTTP assertions made easy via superagent.
??????????????????????????????????????????
????1????????????Java???????NodeJS?????????棬?????????????????????????????????á??????????????
????2??????????棬?????????????????????????????????????????????????????????????????
????3?????????????supertest?????????????????????????????д?????????????????????????????????????????"SuperTest works with any test framework"???????ò?????????
????4???????????????supertest???????nodeJS??????????????????????????node??????????????????????????????С?
????5???????????????????????棬REST-assured??????????maven??grade??????????????????л???????鷳????superset???????????npm install ???????????á??????????????supertest?????????
??????????
?????????supertest??
?????????????github?????supertest????????????
????· ?????superagent???е?API???÷???
????· ?????譎?node???????npm install supertest --save-dev????cnpm install supertest --save-dev???supertest??
????· ??superagent???????????????.end() ??????request????
????· ????.expect()??????????????????????????????????http???????????
???????????????????1??????????????????????δ??????????
????var request = require('supertest');
????var express = require('express');
????var app = express();
?????????app???????????????mock server????supertest?й????????????????
????request(app)
????.get('/user')
????.expect('Content-Type'?? /json/)
????.expect('Content-Length'?? '15')
????.expect(200)
????.end(function(err?? res) {
????if (err) throw err;
????});
??????????β??????????????request(app)????????server???????.expect()????????response header?????content-type??content-length??response??http status???200. ????supertest?????д?????
????С?????
???????????????????????github????????飬???????ж?????????????????????
???????????????????chrome????develop tools??Network??????????????github??????????Щ????????????????????????????????URL??Method?????????

????????Σ?????????????vim?????±????????????????С?δ??????????
????var request = require('supertest')('https://github.com/');
????request
????.get('/')
????.expect(2010)
????.end(function(err?? res) {
????if (err) throw err;
????});
????????????????????test.js??????????????
????node test.js
?????????????????????????

????????????????????????.expect(2010)??????????.expect(200)???????????з?????????????????????
?????????£???????????????????????????????????????Щ???????????????????????????????????С?
????????????????ù??????????????????Mocha???????????????
????Mocha???????JavaScript????????????Jasmine????????????????????
????Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser?? making asynchronous testing simple and fun. Mocha tests run serially?? allowing for flexible and accurate reporting?? while mapping uncaught exceptions to the correct test cases. Hosted on GitHub.
?????????????????style???????檔???supertest???????????????API???????????????????Ρ?
???????????????????post?????????
var request = require('supertest')('https://github.com');
describe('Github home page'??function(){
this.timeout(10000);
before('must be on home page'??function(done){
request.get('/')
.expect(200??done);
});
it('could be navigated to register page'??function(done){
request.get('/join')
.expect(200??done);
});
it('will refuse the request if username has been taken'??function(done){
request.post('/signup_check/username')
.type('form')
.send('value=lala')
.expect(404)
.end(function(err??res){
if (err) return done(err)
done();
})
});
});
?????????????????汾??????п?????????Mocha??????β???????????????????????????????δ????????????
????????before()??Mocha????hook??????beforeAll?????????в??????С?????hook???л??????в???????????е?after()???????????????????????beforeEach()??????????????????????????afterEach()??hook?????????????????????????
???????describe()????????????????????:
????describe('???????????'??function(){
????//????????
????})
??????`describe()`?????`it()`?????????????????????
????it('????????????'?? function(done){
????//???????????
????done();
????})
????done()??Mocha??????????????????done()???Javascript???????????3????????????Mocha????????????2????????describe?????????this.timeout(10000);????????????????10??
?????????????????????Mocha???????????superset??.end()?????????.expect()???done??????????.expect(200??done)??????????????.end()??д???????????????.end()????е???done()??
????????????е?.send('value=lala')??post??request body?????.type()??????????.type()?????????????JSON(???superagent?????)??????????????form????? ?????????????send()????????????post??url?м??????request.post('/signup_check/username?value=lala')??????????????????????????????.send()??
????Mocha??????watch???????????????????mocha -w ??????.js???????????????????б仯?????Mocha????????н????