123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082 |
- <!DOCTYPE html>
- <html>
- <head>
- <title>SuperAgent - Ajax with less suck</title>
- <link rel="stylesheet" href="style.css">
- <script src="jquery.js"></script>
- <script src="jquery-ui.min.js"></script>
- <script src="highlight.js"></script>
- <script src="jquery.tocify.min.js"></script>
- <script>
- $(function(){
- $('#menu').tocify({
- selectors: 'h2',
- hashGenerator: 'pretty'
- });
- });
- </script>
- </head>
- <body>
- <ul id="menu"></ul>
- <div id="content"> <section class="suite">
- <h1>request</h1>
- <dl>
- <section class="suite">
- <h1>with a callback</h1>
- <dl>
- <dt>should invoke .end()</dt>
- <dd><pre><code>request
- .get(uri + '/login', function(err, res){
- assert.equal(res.status, 200);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.end()</h1>
- <dl>
- <dt>should issue a request</dt>
- <dd><pre><code>request
- .get(uri + '/login')
- .end(function(err, res){
- assert.equal(res.status, 200);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.error</h1>
- <dl>
- <dt>should should be an Error object</dt>
- <dd><pre><code>request
- .get(uri + '/error')
- .end(function(err, res){
- if (NODE) {
- res.error.message.should.equal('cannot GET /error (500)');
- }
- else {
- res.error.message.should.equal('cannot GET ' + uri + '/error (500)');
- }
- assert.strictEqual(res.error.status, 500);
- assert(err, 'should have an error for 500');
- assert.equal(err.message, 'Internal Server Error');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.header</h1>
- <dl>
- <dt>should be an object</dt>
- <dd><pre><code>request
- .get(uri + '/login')
- .end(function(err, res){
- assert.equal('Express', res.header['x-powered-by']);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.charset</h1>
- <dl>
- <dt>should be set when present</dt>
- <dd><pre><code>request
- .get(uri + '/login')
- .end(function(err, res){
- res.charset.should.equal('utf-8');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.statusType</h1>
- <dl>
- <dt>should provide the first digit</dt>
- <dd><pre><code>request
- .get(uri + '/login')
- .end(function(err, res){
- assert(!err, 'should not have an error for success responses');
- assert.equal(200, res.status);
- assert.equal(2, res.statusType);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.type</h1>
- <dl>
- <dt>should provide the mime-type void of params</dt>
- <dd><pre><code>request
- .get(uri + '/login')
- .end(function(err, res){
- res.type.should.equal('text/html');
- res.charset.should.equal('utf-8');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.set(field, val)</h1>
- <dl>
- <dt>should set the header field</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .set('X-Foo', 'bar')
- .set('X-Bar', 'baz')
- .end(function(err, res){
- assert.equal('bar', res.header['x-foo']);
- assert.equal('baz', res.header['x-bar']);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.set(obj)</h1>
- <dl>
- <dt>should set the header fields</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .set({ 'X-Foo': 'bar', 'X-Bar': 'baz' })
- .end(function(err, res){
- assert.equal('bar', res.header['x-foo']);
- assert.equal('baz', res.header['x-bar']);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.type(str)</h1>
- <dl>
- <dt>should set the Content-Type</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .type('text/x-foo')
- .end(function(err, res){
- res.header['content-type'].should.equal('text/x-foo');
- done();
- });</code></pre></dd>
- <dt>should map "json"</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .type('json')
- .send('{"a": 1}')
- .end(function(err, res){
- res.should.be.json;
- done();
- });</code></pre></dd>
- <dt>should map "html"</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .type('html')
- .end(function(err, res){
- res.header['content-type'].should.equal('text/html');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.accept(str)</h1>
- <dl>
- <dt>should set Accept</dt>
- <dd><pre><code>request
- .get(uri + '/echo')
- .accept('text/x-foo')
- .end(function(err, res){
- res.header['accept'].should.equal('text/x-foo');
- done();
- });</code></pre></dd>
- <dt>should map "json"</dt>
- <dd><pre><code>request
- .get(uri + '/echo')
- .accept('json')
- .end(function(err, res){
- res.header['accept'].should.equal('application/json');
- done();
- });</code></pre></dd>
- <dt>should map "xml"</dt>
- <dd><pre><code>request
- .get(uri + '/echo')
- .accept('xml')
- .end(function(err, res){
- res.header['accept'].should.equal('application/xml');
- done();
- });</code></pre></dd>
- <dt>should map "html"</dt>
- <dd><pre><code>request
- .get(uri + '/echo')
- .accept('html')
- .end(function(err, res){
- res.header['accept'].should.equal('text/html');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.send(str)</h1>
- <dl>
- <dt>should write the string</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .type('json')
- .send('{"name":"tobi"}')
- .end(function(err, res){
- res.text.should.equal('{"name":"tobi"}');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.send(Object)</h1>
- <dl>
- <dt>should default to json</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .send({ name: 'tobi' })
- .end(function(err, res){
- res.should.be.json
- res.text.should.equal('{"name":"tobi"}');
- done();
- });</code></pre></dd>
- <section class="suite">
- <h1>when called several times</h1>
- <dl>
- <dt>should merge the objects</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .send({ name: 'tobi' })
- .send({ age: 1 })
- .end(function(err, res){
- res.should.be.json
- if (NODE) {
- res.buffered.should.be.true;
- }
- res.text.should.equal('{"name":"tobi","age":1}');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>.end(fn)</h1>
- <dl>
- <dt>should check arity</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .send({ name: 'tobi' })
- .end(function(err, res){
- assert.equal(null, err);
- res.text.should.equal('{"name":"tobi"}');
- done();
- });</code></pre></dd>
- <dt>should emit request</dt>
- <dd><pre><code>var req = request.post(uri + '/echo');
- req.on('request', function(request){
- assert.equal(req, request);
- done();
- });
- req.end();</code></pre></dd>
- <dt>should emit response</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .send({ name: 'tobi' })
- .on('response', function(res){
- res.text.should.equal('{"name":"tobi"}');
- done();
- })
- .end();</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.then(fulfill, reject)</h1>
- <dl>
- <dt>should support successful fulfills with .then(fulfill)</dt>
- <dd><pre><code>request
- .post(uri + '/echo')
- .send({ name: 'tobi' })
- .then(function(res) {
- res.text.should.equal('{"name":"tobi"}');
- done();
- })</code></pre></dd>
- <dt>should reject an error with .then(null, reject)</dt>
- <dd><pre><code>request
- .get(uri + '/error')
- .then(null, function(err) {
- assert.equal(err.status, 500);
- assert.equal(err.response.text, 'boom');
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.abort()</h1>
- <dl>
- <dt>should abort the request</dt>
- <dd><pre><code>var req = request
- .get(uri + '/delay/3000')
- .end(function(err, res){
- assert(false, 'should not complete the request');
- });
- req.on('abort', done);
- setTimeout(function() {
- req.abort();
- }, 1000);</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>request</h1>
- <dl>
- <section class="suite">
- <h1>persistent agent</h1>
- <dl>
- <dt>should gain a session on POST</dt>
- <dd><pre><code>agent3
- .post('http://localhost:4000/signin')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- should.not.exist(res.headers['set-cookie']);
- res.text.should.containEql('dashboard');
- done();
- });</code></pre></dd>
- <dt>should start with empty session (set cookies)</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/dashboard')
- .end(function(err, res) {
- should.exist(err);
- res.should.have.status(401);
- should.exist(res.headers['set-cookie']);
- done();
- });</code></pre></dd>
- <dt>should gain a session (cookies already set)</dt>
- <dd><pre><code>agent1
- .post('http://localhost:4000/signin')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- should.not.exist(res.headers['set-cookie']);
- res.text.should.containEql('dashboard');
- done();
- });</code></pre></dd>
- <dt>should persist cookies across requests</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/dashboard')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- done();
- });</code></pre></dd>
- <dt>should have the cookie set in the end callback</dt>
- <dd><pre><code>agent4
- .post('http://localhost:4000/setcookie')
- .end(function(err, res) {
- agent4
- .get('http://localhost:4000/getcookie')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- assert.strictEqual(res.text, 'jar');
- done();
- });
- });</code></pre></dd>
- <dt>should not share cookies</dt>
- <dd><pre><code>agent2
- .get('http://localhost:4000/dashboard')
- .end(function(err, res) {
- should.exist(err);
- res.should.have.status(401);
- done();
- });</code></pre></dd>
- <dt>should not lose cookies between agents</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/dashboard')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- done();
- });</code></pre></dd>
- <dt>should be able to follow redirects</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- res.text.should.containEql('dashboard');
- done();
- });</code></pre></dd>
- <dt>should be able to post redirects</dt>
- <dd><pre><code>agent1
- .post('http://localhost:4000/redirect')
- .send({ foo: 'bar', baz: 'blaaah' })
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- res.text.should.containEql('simple');
- res.redirects.should.eql(['http://localhost:4000/simple']);
- done();
- });</code></pre></dd>
- <dt>should be able to limit redirects</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/')
- .redirects(0)
- .end(function(err, res) {
- should.exist(err);
- res.should.have.status(302);
- res.redirects.should.eql([]);
- res.header.location.should.equal('/dashboard');
- done();
- });</code></pre></dd>
- <dt>should be able to create a new session (clear cookie)</dt>
- <dd><pre><code>agent1
- .post('http://localhost:4000/signout')
- .end(function(err, res) {
- should.not.exist(err);
- res.should.have.status(200);
- should.exist(res.headers['set-cookie']);
- done();
- });</code></pre></dd>
- <dt>should regenerate with an empty session</dt>
- <dd><pre><code>agent1
- .get('http://localhost:4000/dashboard')
- .end(function(err, res) {
- should.exist(err);
- res.should.have.status(401);
- should.not.exist(res.headers['set-cookie']);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>Basic auth</h1>
- <dl>
- <section class="suite">
- <h1>when credentials are present in url</h1>
- <dl>
- <dt>should set Authorization</dt>
- <dd><pre><code>request
- .get('http://tobi:learnboost@localhost:3010')
- .end(function(err, res){
- res.status.should.equal(200);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.auth(user, pass)</h1>
- <dl>
- <dt>should set Authorization</dt>
- <dd><pre><code>request
- .get('http://localhost:3010')
- .auth('tobi', 'learnboost')
- .end(function(err, res){
- res.status.should.equal(200);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.auth(user + ":" + pass)</h1>
- <dl>
- <dt>should set authorization</dt>
- <dd><pre><code>request
- .get('http://localhost:3010/again')
- .auth('tobi')
- .end(function(err, res){
- res.status.should.eql(200);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>[node] request</h1>
- <dl>
- <section class="suite">
- <h1>res.statusCode</h1>
- <dl>
- <dt>should set statusCode</dt>
- <dd><pre><code>request
- .get('http://localhost:5000/login', function(err, res){
- assert.strictEqual(res.statusCode, 200);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with an object</h1>
- <dl>
- <dt>should format the url</dt>
- <dd><pre><code>request
- .get(url.parse('http://localhost:5000/login'))
- .end(function(err, res){
- assert(res.ok);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>without a schema</h1>
- <dl>
- <dt>should default to http</dt>
- <dd><pre><code>request
- .get('localhost:5000/login')
- .end(function(err, res){
- assert.equal(res.status, 200);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.toJSON()</h1>
- <dl>
- <dt>should describe the request</dt>
- <dd><pre><code>request
- .post(':5000/echo')
- .send({ foo: 'baz' })
- .end(function(err, res){
- var obj = res.request.toJSON();
- assert.equal('POST', obj.method);
- assert.equal(':5000/echo', obj.url);
- assert.equal('baz', obj.data.foo);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>should allow the send shorthand</h1>
- <dl>
- <dt>with callback in the method call</dt>
- <dd><pre><code>request
- .get('http://localhost:5000/login', function(err, res) {
- assert.equal(res.status, 200);
- done();
- });</code></pre></dd>
- <dt>with data in the method call</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo', { foo: 'bar' })
- .end(function(err, res) {
- assert.equal('{"foo":"bar"}', res.text);
- done();
- });</code></pre></dd>
- <dt>with callback and data in the method call</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo', { foo: 'bar' }, function(err, res) {
- assert.equal('{"foo":"bar"}', res.text);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.toJSON()</h1>
- <dl>
- <dt>should describe the response</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo')
- .send({ foo: 'baz' })
- .end(function(err, res){
- var obj = res.toJSON();
- assert.equal('object', typeof obj.header);
- assert.equal('object', typeof obj.req);
- assert.equal(200, obj.status);
- assert.equal('{"foo":"baz"}', obj.text);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.links</h1>
- <dl>
- <dt>should default to an empty object</dt>
- <dd><pre><code>request
- .get('http://localhost:5000/login')
- .end(function(err, res){
- res.links.should.eql({});
- done();
- })</code></pre></dd>
- <dt>should parse the Link header field</dt>
- <dd><pre><code>request
- .get('http://localhost:5000/links')
- .end(function(err, res){
- res.links.next.should.equal('https://api.github.com/repos/visionmedia/mocha/issues?page=2');
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.unset(field)</h1>
- <dl>
- <dt>should remove the header field</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo')
- .unset('User-Agent')
- .end(function(err, res){
- assert.equal(void 0, res.header['user-agent']);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.write(str)</h1>
- <dl>
- <dt>should write the given data</dt>
- <dd><pre><code>var req = request.post('http://localhost:5000/echo');
- req.set('Content-Type', 'application/json');
- req.write('{"name"').should.be.a.boolean;
- req.write(':"tobi"}').should.be.a.boolean;
- req.end(function(err, res){
- res.text.should.equal('{"name":"tobi"}');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.pipe(stream)</h1>
- <dl>
- <dt>should pipe the response to the given stream</dt>
- <dd><pre><code>var stream = new EventEmitter;
- stream.buf = '';
- stream.writable = true;
- stream.write = function(chunk){
- this.buf += chunk;
- };
- stream.end = function(){
- this.buf.should.equal('{"name":"tobi"}');
- done();
- };
- request
- .post('http://localhost:5000/echo')
- .send('{"name":"tobi"}')
- .pipe(stream);</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.buffer()</h1>
- <dl>
- <dt>should enable buffering</dt>
- <dd><pre><code>request
- .get('http://localhost:5000/custom')
- .buffer()
- .end(function(err, res){
- assert.equal(null, err);
- assert.equal('custom stuff', res.text);
- assert(res.buffered);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.buffer(false)</h1>
- <dl>
- <dt>should disable buffering</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo')
- .type('application/x-dog')
- .send('hello this is dog')
- .buffer(false)
- .end(function(err, res){
- assert.equal(null, err);
- assert.equal(null, res.text);
- res.body.should.eql({});
- var buf = '';
- res.setEncoding('utf8');
- res.on('data', function(chunk){ buf += chunk });
- res.on('end', function(){
- buf.should.equal('hello this is dog');
- done();
- });
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.agent()</h1>
- <dl>
- <dt>should return the defaut agent</dt>
- <dd><pre><code>var req = request.post('http://localhost:5000/echo');
- req.agent().should.equal(false);
- done();</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.agent(undefined)</h1>
- <dl>
- <dt>should set an agent to undefined and ensure it is chainable</dt>
- <dd><pre><code>var req = request.get('http://localhost:5000/echo');
- var ret = req.agent(undefined);
- ret.should.equal(req);
- assert.strictEqual(req.agent(), undefined);
- done();</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.agent(new http.Agent())</h1>
- <dl>
- <dt>should set passed agent</dt>
- <dd><pre><code>var http = require('http');
- var req = request.get('http://localhost:5000/echo');
- var agent = new http.Agent();
- var ret = req.agent(agent);
- ret.should.equal(req);
- req.agent().should.equal(agent)
- done();</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with a content type other than application/json or text/*</h1>
- <dl>
- <dt>should disable buffering</dt>
- <dd><pre><code>request
- .post('http://localhost:5000/echo')
- .type('application/x-dog')
- .send('hello this is dog')
- .end(function(err, res){
- assert.equal(null, err);
- assert.equal(null, res.text);
- res.body.should.eql({});
- var buf = '';
- res.setEncoding('utf8');
- res.buffered.should.be.false;
- res.on('data', function(chunk){ buf += chunk });
- res.on('end', function(){
- buf.should.equal('hello this is dog');
- done();
- });
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>content-length</h1>
- <dl>
- <dt>should be set to the byte length of a non-buffer object</dt>
- <dd><pre><code>var decoder = new StringDecoder('utf8');
- var img = fs.readFileSync(__dirname + '/fixtures/test.png');
- img = decoder.write(img);
- request
- .post('http://localhost:5000/echo')
- .type('application/x-image')
- .send(img)
- .buffer(false)
- .end(function(err, res){
- assert.equal(null, err);
- assert(!res.buffered);
- assert.equal(res.header['content-length'], Buffer.byteLength(img));
- done();
- });</code></pre></dd>
- <dt>should be set to the length of a buffer object</dt>
- <dd><pre><code>var img = fs.readFileSync(__dirname + '/fixtures/test.png');
- request
- .post('http://localhost:5000/echo')
- .type('application/x-image')
- .send(img)
- .buffer(true)
- .end(function(err, res){
- assert.equal(null, err);
- assert(res.buffered);
- assert.equal(res.header['content-length'], img.length);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.set("Content-Type", contentType)</h1>
- <dl>
- <dt>should work with just the contentType component</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .set('Content-Type', 'application/json')
- .send({ name: 'tobi' })
- .end(function(err, res){
- assert(!err);
- done();
- });</code></pre></dd>
- <dt>should work with the charset component</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .set('Content-Type', 'application/json; charset=utf-8')
- .send({ name: 'tobi' })
- .end(function(err, res){
- assert(!err);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>exports</h1>
- <dl>
- <dt>should expose Part</dt>
- <dd><pre><code>request.Part.should.be.a.function;</code></pre></dd>
- <dt>should expose .protocols</dt>
- <dd><pre><code>Object.keys(request.protocols)
- .should.eql(['http:', 'https:']);</code></pre></dd>
- <dt>should expose .serialize</dt>
- <dd><pre><code>Object.keys(request.serialize)
- .should.eql(['application/x-www-form-urlencoded', 'application/json']);</code></pre></dd>
- <dt>should expose .parse</dt>
- <dd><pre><code>Object.keys(request.parse)
- .should.eql(['application/x-www-form-urlencoded', 'application/json', 'text', 'image']);</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>flags</h1>
- <dl>
- <section class="suite">
- <h1>with 4xx response</h1>
- <dl>
- <dt>should set res.error and res.clientError</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/notfound')
- .end(function(err, res){
- assert(err);
- assert(!res.ok, 'response should not be ok');
- assert(res.error, 'response should be an error');
- assert(res.clientError, 'response should be a client error');
- assert(!res.serverError, 'response should not be a server error');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 5xx response</h1>
- <dl>
- <dt>should set res.error and res.serverError</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/error')
- .end(function(err, res){
- assert(err);
- assert(!res.ok, 'response should not be ok');
- assert(!res.notFound, 'response should not be notFound');
- assert(res.error, 'response should be an error');
- assert(!res.clientError, 'response should not be a client error');
- assert(res.serverError, 'response should be a server error');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 404 Not Found</h1>
- <dl>
- <dt>should res.notFound</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/notfound')
- .end(function(err, res){
- assert(err);
- assert(res.notFound, 'response should be .notFound');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 400 Bad Request</h1>
- <dl>
- <dt>should set req.badRequest</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/bad-request')
- .end(function(err, res){
- assert(err);
- assert(res.badRequest, 'response should be .badRequest');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 401 Bad Request</h1>
- <dl>
- <dt>should set res.unauthorized</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/unauthorized')
- .end(function(err, res){
- assert(err);
- assert(res.unauthorized, 'response should be .unauthorized');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 406 Not Acceptable</h1>
- <dl>
- <dt>should set res.notAcceptable</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/not-acceptable')
- .end(function(err, res){
- assert(err);
- assert(res.notAcceptable, 'response should be .notAcceptable');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>with 204 No Content</h1>
- <dl>
- <dt>should set res.noContent</dt>
- <dd><pre><code>request
- .get('http://localhost:3004/no-content')
- .end(function(err, res){
- assert(!err);
- assert(res.noContent, 'response should be .noContent');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.send(Object) as "form"</h1>
- <dl>
- <section class="suite">
- <h1>with req.type() set to form</h1>
- <dl>
- <dt>should send x-www-form-urlencoded data</dt>
- <dd><pre><code>request
- .post('http://localhost:3002/echo')
- .type('form')
- .send({ name: 'tobi' })
- .end(function(err, res){
- res.header['content-type'].should.equal('application/x-www-form-urlencoded');
- res.text.should.equal('name=tobi');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>when called several times</h1>
- <dl>
- <dt>should merge the objects</dt>
- <dd><pre><code>request
- .post('http://localhost:3002/echo')
- .type('form')
- .send({ name: { first: 'tobi', last: 'holowaychuk' } })
- .send({ age: '1' })
- .end(function(err, res){
- res.header['content-type'].should.equal('application/x-www-form-urlencoded');
- res.text.should.equal('name%5Bfirst%5D=tobi&name%5Blast%5D=holowaychuk&age=1');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.send(String)</h1>
- <dl>
- <dt>should default to "form"</dt>
- <dd><pre><code>request
- .post('http://localhost:3002/echo')
- .send('user[name]=tj')
- .send('user[email]=tj@vision-media.ca')
- .end(function(err, res){
- res.header['content-type'].should.equal('application/x-www-form-urlencoded');
- res.body.should.eql({ user: { name: 'tj', email: 'tj@vision-media.ca' } });
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>res.body</h1>
- <dl>
- <section class="suite">
- <h1>application/x-www-form-urlencoded</h1>
- <dl>
- <dt>should parse the body</dt>
- <dd><pre><code>request
- .get('http://localhost:3002/form-data')
- .end(function(err, res){
- res.text.should.equal('pet[name]=manny');
- res.body.should.eql({ pet: { name: 'manny' }});
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>https</h1>
- <dl>
- <section class="suite">
- <h1>request</h1>
- <dl>
- <dt>should give a good response</dt>
- <dd><pre><code>request
- .get('https://localhost:8443/')
- .ca(cert)
- .end(function(err, res){
- assert(res.ok);
- assert.strictEqual('Safe and secure!', res.text);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.agent</h1>
- <dl>
- <dt>should be able to make multiple requests without redefining the certificate</dt>
- <dd><pre><code>var agent = request.agent({ca: cert});
- agent
- .get('https://localhost:8443/')
- .end(function(err, res){
- assert(res.ok);
- assert.strictEqual('Safe and secure!', res.text);
- agent
- .get(url.parse('https://localhost:8443/'))
- .end(function(err, res){
- assert(res.ok);
- assert.strictEqual('Safe and secure!', res.text);
- done();
- });
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>res.body</h1>
- <dl>
- <section class="suite">
- <h1>image/png</h1>
- <dl>
- <dt>should parse the body</dt>
- <dd><pre><code>request
- .get('http://localhost:3011/image')
- .end(function(err, res){
- (res.body.length - img.length).should.equal(0);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>zlib</h1>
- <dl>
- <dt>should deflate the content</dt>
- <dd><pre><code>request
- .get('http://localhost:3080')
- .end(function(err, res){
- res.should.have.status(200);
- res.text.should.equal(subject);
- res.headers['content-length'].should.be.below(subject.length);
- done();
- });</code></pre></dd>
- <dt>should handle corrupted responses</dt>
- <dd><pre><code>request
- .get('http://localhost:3080/corrupt')
- .end(function(err, res){
- assert(err, 'missing error');
- assert(!res, 'response should not be defined');
- done();
- });</code></pre></dd>
- <section class="suite">
- <h1>without encoding set</h1>
- <dl>
- <dt>should emit buffers</dt>
- <dd><pre><code>request
- .get('http://localhost:3080/binary')
- .end(function(err, res){
- res.should.have.status(200);
- res.headers['content-length'].should.be.below(subject.length);
- res.on('data', function(chunk){
- chunk.should.have.length(subject.length);
- });
- res.on('end', done);
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.send(Object) as "json"</h1>
- <dl>
- <dt>should default to json</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .send({ name: 'tobi' })
- .end(function(err, res){
- res.should.be.json
- res.text.should.equal('{"name":"tobi"}');
- done();
- });</code></pre></dd>
- <dt>should work with arrays</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .send([1,2,3])
- .end(function(err, res){
- res.should.be.json
- res.text.should.equal('[1,2,3]');
- done();
- });</code></pre></dd>
- <dt>should work with value null</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .type('json')
- .send('null')
- .end(function(err, res){
- res.should.be.json
- assert.strictEqual(res.body, null);
- done();
- });</code></pre></dd>
- <dt>should work with value false</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .type('json')
- .send('false')
- .end(function(err, res){
- res.should.be.json
- res.body.should.equal(false);
- done();
- });</code></pre></dd>
- <dt>should work with value 0</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .type('json')
- .send('0')
- .end(function(err, res){
- res.should.be.json
- res.body.should.equal(0);
- done();
- });</code></pre></dd>
- <dt>should work with empty string value</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .type('json')
- .send('""')
- .end(function(err, res){
- res.should.be.json
- res.body.should.equal("");
- done();
- });</code></pre></dd>
- <dt>should work with GET</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/echo')
- .send({ tobi: 'ferret' })
- .end(function(err, res){
- res.should.be.json
- res.text.should.equal('{"tobi":"ferret"}');
- done();
- });</code></pre></dd>
- <dt>should work with vendor MIME type</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .set('Content-Type', 'application/vnd.example+json')
- .send({ name: 'vendor' })
- .end(function(err, res){
- res.text.should.equal('{"name":"vendor"}');
- done();
- });</code></pre></dd>
- <section class="suite">
- <h1>when called several times</h1>
- <dl>
- <dt>should merge the objects</dt>
- <dd><pre><code>request
- .post('http://localhost:3005/echo')
- .send({ name: 'tobi' })
- .send({ age: 1 })
- .end(function(err, res){
- res.should.be.json
- res.text.should.equal('{"name":"tobi","age":1}');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>res.body</h1>
- <dl>
- <section class="suite">
- <h1>application/json</h1>
- <dl>
- <dt>should parse the body</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/json')
- .end(function(err, res){
- res.text.should.equal('{"name":"manny"}');
- res.body.should.eql({ name: 'manny' });
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>HEAD requests</h1>
- <dl>
- <dt>should not throw a parse error</dt>
- <dd><pre><code>request
- .head('http://localhost:3005/json')
- .end(function(err, res){
- assert.strictEqual(err, null);
- assert.strictEqual(res.text, undefined)
- assert.strictEqual(Object.keys(res.body).length, 0)
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>Invalid JSON response</h1>
- <dl>
- <dt>should return the raw response</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/invalid-json')
- .end(function(err, res){
- assert.deepEqual(err.rawResponse, ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>No content</h1>
- <dl>
- <dt>should not throw a parse error</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/no-content')
- .end(function(err, res){
- assert.strictEqual(err, null);
- assert.strictEqual(res.text, '');
- assert.strictEqual(Object.keys(res.body).length, 0);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>application/json+hal</h1>
- <dl>
- <dt>should parse the body</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/json-hal')
- .end(function(err, res){
- if (err) return done(err);
- res.text.should.equal('{"name":"hal 5000"}');
- res.body.should.eql({ name: 'hal 5000' });
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>vnd.collection+json</h1>
- <dl>
- <dt>should parse the body</dt>
- <dd><pre><code>request
- .get('http://localhost:3005/collection-json')
- .end(function(err, res){
- res.text.should.equal('{"name":"chewbacca"}');
- res.body.should.eql({ name: 'chewbacca' });
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>Request</h1>
- <dl>
- <section class="suite">
- <h1>#attach(name, path, filename)</h1>
- <dl>
- <dt>should use the custom filename</dt>
- <dd><pre><code>request
- .post(':3005/echo')
- .attach('document', 'test/node/fixtures/user.html', 'doc.html')
- .end(function(err, res){
- if (err) return done(err);
- var html = res.files.document;
- html.name.should.equal('doc.html');
- html.type.should.equal('text/html');
- read(html.path).should.equal('<h1>name</h1>');
- done();
- })</code></pre></dd>
- <dt>should fire progress event</dt>
- <dd><pre><code>var loaded = 0;
- var total = 0;
- request
- .post(':3005/echo')
- .attach('document', 'test/node/fixtures/user.html')
- .on('progress', function (event) {
- total = event.total;
- loaded = event.loaded;
- })
- .end(function(err, res){
- if (err) return done(err);
- var html = res.files.document;
- html.name.should.equal('user.html');
- html.type.should.equal('text/html');
- read(html.path).should.equal('<h1>name</h1>');
- total.should.equal(221);
- loaded.should.equal(221);
- done();
- })</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>with network error</h1>
- <dl>
- <dt>should error</dt>
- <dd><pre><code>request
- .get('http://localhost:' + this.port + '/')
- .end(function(err, res){
- assert(err, 'expected an error');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>request</h1>
- <dl>
- <section class="suite">
- <h1>not modified</h1>
- <dl>
- <dt>should start with 200</dt>
- <dd><pre><code>request
- .get('http://localhost:3008/')
- .end(function(err, res){
- res.should.have.status(200)
- res.text.should.match(/^\d+$/);
- ts = +res.text;
- done();
- });</code></pre></dd>
- <dt>should then be 304</dt>
- <dd><pre><code>request
- .get('http://localhost:3008/')
- .set('If-Modified-Since', new Date(ts).toUTCString())
- .end(function(err, res){
- res.should.have.status(304)
- // res.text.should.be.empty
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.parse(fn)</h1>
- <dl>
- <dt>should take precedence over default parsers</dt>
- <dd><pre><code>request
- .get('http://localhost:3033/manny')
- .parse(request.parse['application/json'])
- .end(function(err, res){
- assert(res.ok);
- assert.equal('{"name":"manny"}', res.text);
- assert.equal('manny', res.body.name);
- done();
- });</code></pre></dd>
- <dt>should be the only parser</dt>
- <dd><pre><code>request
- .get('http://localhost:3033/image')
- .parse(function(res, fn) {
- res.on('data', function() {});
- })
- .end(function(err, res){
- assert(res.ok);
- assert.strictEqual(res.text, undefined);
- res.body.should.eql({});
- done();
- });</code></pre></dd>
- <dt>should emit error if parser throws</dt>
- <dd><pre><code>request
- .get('http://localhost:3033/manny')
- .parse(function() {
- throw new Error('I am broken');
- })
- .on('error', function(err) {
- err.message.should.equal('I am broken');
- done();
- })
- .end();</code></pre></dd>
- <dt>should emit error if parser returns an error</dt>
- <dd><pre><code>request
- .get('http://localhost:3033/manny')
- .parse(function(res, fn) {
- fn(new Error('I am broken'));
- })
- .on('error', function(err) {
- err.message.should.equal('I am broken');
- done();
- })
- .end()</code></pre></dd>
- <dt>should not emit error on chunked json</dt>
- <dd><pre><code>request
- .get('http://localhost:3033/chunked-json')
- .end(function(err){
- assert(!err);
- done();
- });</code></pre></dd>
- <dt>should not emit error on aborted chunked json</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3033/chunked-json')
- .end(function(err){
- assert(!err);
- done();
- });
- setTimeout(function(){req.abort()},50);</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>pipe on redirect</h1>
- <dl>
- <dt>should follow Location</dt>
- <dd><pre><code>var stream = fs.createWriteStream('test/node/fixtures/pipe.txt');
- var redirects = [];
- var req = request
- .get('http://localhost:3012/')
- .on('redirect', function (res) {
- redirects.push(res.headers.location);
- })
- .on('end', function () {
- var arr = [];
- arr.push('/movies');
- arr.push('/movies/all');
- arr.push('/movies/all/0');
- redirects.should.eql(arr);
- fs.readFileSync('test/node/fixtures/pipe.txt', 'utf8').should.eql('first movie page');
- done();
- });
- req.pipe(stream);</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>request pipe</h1>
- <dl>
- <dt>should act as a writable stream</dt>
- <dd><pre><code>var req = request.post('http://localhost:3020');
- var stream = fs.createReadStream('test/node/fixtures/user.json');
- req.type('json');
- req.on('response', function(res){
- res.body.should.eql({ name: 'tobi' });
- done();
- });
- stream.pipe(req);</code></pre></dd>
- <dt>should act as a readable stream</dt>
- <dd><pre><code>var stream = fs.createWriteStream('test/node/fixtures/tmp.json');
- var req = request.get('http://localhost:3025');
- req.type('json');
- req.on('end', function(){
- JSON.parse(fs.readFileSync('test/node/fixtures/tmp.json', 'utf8')).should.eql({ name: 'tobi' });
- done();
- });
- req.pipe(stream);</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.query(String)</h1>
- <dl>
- <dt>should supply uri malformed error to the callback</dt>
- <dd><pre><code>request
- .get('http://localhost:3006')
- .query('name=toby')
- .query('a=\uD800')
- .query({ b: '\uD800' })
- .end(function(err, res){
- assert(err instanceof Error);
- assert.equal('URIError', err.name);
- done();
- });</code></pre></dd>
- <dt>should support passing in a string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006')
- .query('name=t%F6bi')
- .end(function(err, res){
- res.body.should.eql({ name: 't%F6bi' });
- done();
- });</code></pre></dd>
- <dt>should work with url query-string and string for query</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/?name=tobi')
- .query('age=2%20')
- .end(function(err, res){
- res.body.should.eql({ name: 'tobi', age: '2 ' });
- done();
- });</code></pre></dd>
- <dt>should support compound elements in a string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/')
- .query('name=t%F6bi&age=2')
- .end(function(err, res){
- res.body.should.eql({ name: 't%F6bi', age: '2' });
- done();
- });</code></pre></dd>
- <dt>should work when called multiple times with a string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/')
- .query('name=t%F6bi')
- .query('age=2%F6')
- .end(function(err, res){
- res.body.should.eql({ name: 't%F6bi', age: '2%F6' });
- done();
- });</code></pre></dd>
- <dt>should work with normal `query` object and query string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/')
- .query('name=t%F6bi')
- .query({ age: '2' })
- .end(function(err, res){
- res.body.should.eql({ name: 't%F6bi', age: '2' });
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.query(Object)</h1>
- <dl>
- <dt>should construct the query-string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/')
- .query({ name: 'tobi' })
- .query({ order: 'asc' })
- .query({ limit: ['1', '2'] })
- .end(function(err, res){
- res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] });
- done();
- });</code></pre></dd>
- <dt>should not error on dates</dt>
- <dd><pre><code>var date = new Date(0);
- request
- .del('http://localhost:3006/')
- .query({ at: date })
- .end(function(err, res){
- assert.equal(date.toISOString(), res.body.at);
- done();
- });</code></pre></dd>
- <dt>should work after setting header fields</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/')
- .set('Foo', 'bar')
- .set('Bar', 'baz')
- .query({ name: 'tobi' })
- .query({ order: 'asc' })
- .query({ limit: ['1', '2'] })
- .end(function(err, res){
- res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] });
- done();
- });</code></pre></dd>
- <dt>should append to the original query-string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/?name=tobi')
- .query({ order: 'asc' })
- .end(function(err, res) {
- res.body.should.eql({ name: 'tobi', order: 'asc' });
- done();
- });</code></pre></dd>
- <dt>should retain the original query-string</dt>
- <dd><pre><code>request
- .del('http://localhost:3006/?name=tobi')
- .end(function(err, res) {
- res.body.should.eql({ name: 'tobi' });
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>request.get</h1>
- <dl>
- <section class="suite">
- <h1>on 301 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3210/test-301')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 302 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3210/test-302')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 303 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3210/test-303')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 307 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3210/test-307')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 308 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3210/test-308')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>request.post</h1>
- <dl>
- <section class="suite">
- <h1>on 301 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .post('http://localhost:3210/test-301')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 302 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .post('http://localhost:3210/test-302')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 303 redirect</h1>
- <dl>
- <dt>should follow Location with a GET request</dt>
- <dd><pre><code>var req = request
- .post('http://localhost:3210/test-303')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('GET');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 307 redirect</h1>
- <dl>
- <dt>should follow Location with a POST request</dt>
- <dd><pre><code>var req = request
- .post('http://localhost:3210/test-307')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('POST');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 308 redirect</h1>
- <dl>
- <dt>should follow Location with a POST request</dt>
- <dd><pre><code>var req = request
- .post('http://localhost:3210/test-308')
- .redirects(1)
- .end(function(err, res){
- req.req._headers.host.should.eql('localhost:3211');
- res.status.should.eql(200);
- res.text.should.eql('POST');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>request</h1>
- <dl>
- <section class="suite">
- <h1>on redirect</h1>
- <dl>
- <dt>should follow Location</dt>
- <dd><pre><code>var redirects = [];
- request
- .get('http://localhost:3003/')
- .on('redirect', function(res){
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- arr.push('/movies');
- arr.push('/movies/all');
- arr.push('/movies/all/0');
- redirects.should.eql(arr);
- res.text.should.equal('first movie page');
- done();
- });</code></pre></dd>
- <dt>should retain header fields</dt>
- <dd><pre><code>request
- .get('http://localhost:3003/header')
- .set('X-Foo', 'bar')
- .end(function(err, res){
- res.body.should.have.property('x-foo', 'bar');
- done();
- });</code></pre></dd>
- <dt>should remove Content-* fields</dt>
- <dd><pre><code>request
- .post('http://localhost:3003/header')
- .type('txt')
- .set('X-Foo', 'bar')
- .set('X-Bar', 'baz')
- .send('hey')
- .end(function(err, res){
- res.body.should.have.property('x-foo', 'bar');
- res.body.should.have.property('x-bar', 'baz');
- res.body.should.not.have.property('content-type');
- res.body.should.not.have.property('content-length');
- res.body.should.not.have.property('transfer-encoding');
- done();
- });</code></pre></dd>
- <dt>should retain cookies</dt>
- <dd><pre><code>request
- .get('http://localhost:3003/header')
- .set('Cookie', 'foo=bar;')
- .end(function(err, res){
- res.body.should.have.property('cookie', 'foo=bar;');
- done();
- });</code></pre></dd>
- <dt>should preserve timeout across redirects</dt>
- <dd><pre><code>request
- .get('http://localhost:3003/movies/random')
- .timeout(250)
- .end(function(err, res){
- assert(err instanceof Error, 'expected an error');
- err.should.have.property('timeout', 250);
- done();
- });</code></pre></dd>
- <dt>should not resend query parameters</dt>
- <dd><pre><code>var redirects = [];
- var query = [];
- request
- .get('http://localhost:3003/?foo=bar')
- .on('redirect', function(res){
- query.push(res.headers.query);
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- arr.push('/movies');
- arr.push('/movies/all');
- arr.push('/movies/all/0');
- redirects.should.eql(arr);
- res.text.should.equal('first movie page');
- query.should.eql(['{"foo":"bar"}', '{}', '{}']);
- res.headers.query.should.eql('{}');
- done();
- });</code></pre></dd>
- <dt>should handle no location header</dt>
- <dd><pre><code>request
- .get('http://localhost:3003/bad-redirect')
- .end(function(err, res){
- err.message.should.equal('No location header for redirect');
- done();
- });</code></pre></dd>
- <section class="suite">
- <h1>when relative</h1>
- <dl>
- <dt>should redirect to a sibling path</dt>
- <dd><pre><code>var redirects = [];
- request
- .get('http://localhost:3003/relative')
- .on('redirect', function(res){
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- redirects.should.eql(['tobi']);
- res.text.should.equal('tobi');
- done();
- });</code></pre></dd>
- <dt>should redirect to a parent path</dt>
- <dd><pre><code>var redirects = [];
- request
- .get('http://localhost:3003/relative/sub')
- .on('redirect', function(res){
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- redirects.should.eql(['../tobi']);
- res.text.should.equal('tobi');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>req.redirects(n)</h1>
- <dl>
- <dt>should alter the default number of redirects to follow</dt>
- <dd><pre><code>var redirects = [];
- request
- .get('http://localhost:3003/')
- .redirects(2)
- .on('redirect', function(res){
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- assert(res.redirect, 'res.redirect');
- arr.push('/movies');
- arr.push('/movies/all');
- redirects.should.eql(arr);
- res.text.should.match(/Moved Temporarily|Found/);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on POST</h1>
- <dl>
- <dt>should redirect as GET</dt>
- <dd><pre><code>var redirects = [];
- request
- .post('http://localhost:3003/movie')
- .send({ name: 'Tobi' })
- .redirects(2)
- .on('redirect', function(res){
- redirects.push(res.headers.location);
- })
- .end(function(err, res){
- var arr = [];
- arr.push('/movies/all/0');
- redirects.should.eql(arr);
- res.text.should.equal('first movie page');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 303</h1>
- <dl>
- <dt>should redirect with same method</dt>
- <dd><pre><code>request
- .put('http://localhost:3003/redirect-303')
- .send({msg: "hello"})
- .redirects(1)
- .on('redirect', function(res) {
- res.headers.location.should.equal('/reply-method')
- })
- .end(function(err, res){
- res.text.should.equal('method=get');
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 307</h1>
- <dl>
- <dt>should redirect with same method</dt>
- <dd><pre><code>request
- .put('http://localhost:3003/redirect-307')
- .send({msg: "hello"})
- .redirects(1)
- .on('redirect', function(res) {
- res.headers.location.should.equal('/reply-method')
- })
- .end(function(err, res){
- res.text.should.equal('method=put');
- done();
- })</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>on 308</h1>
- <dl>
- <dt>should redirect with same method</dt>
- <dd><pre><code>request
- .put('http://localhost:3003/redirect-308')
- .send({msg: "hello"})
- .redirects(1)
- .on('redirect', function(res) {
- res.headers.location.should.equal('/reply-method')
- })
- .end(function(err, res){
- res.text.should.equal('method=put');
- done();
- })</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>response</h1>
- <dl>
- <dt>should act as a readable stream</dt>
- <dd><pre><code>var req = request
- .get('http://localhost:3025')
- .buffer(false);
- req.end(function(err,res){
- if (err) return done(err);
- var trackEndEvent = 0;
- var trackCloseEvent = 0;
- res.on('end',function(){
- trackEndEvent++;
- trackEndEvent.should.equal(1);
- trackCloseEvent.should.equal(0); // close should not have been called
- done();
- });
- res.on('close',function(){
- trackCloseEvent++;
- });
- (function(){ res.pause() }).should.not.throw();
- (function(){ res.resume() }).should.not.throw();
- (function(){ res.destroy() }).should.not.throw();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>.timeout(ms)</h1>
- <dl>
- <section class="suite">
- <h1>when timeout is exceeded</h1>
- <dl>
- <dt>should error</dt>
- <dd><pre><code>request
- .get('http://localhost:3009/500')
- .timeout(150)
- .end(function(err, res){
- assert(err, 'expected an error');
- assert.equal('number', typeof err.timeout, 'expected an error with .timeout');
- assert.equal('ECONNABORTED', err.code, 'expected abort error code')
- done();
- });</code></pre></dd>
- </dl>
- </section>
- </dl>
- </section>
- <section class="suite">
- <h1>res.toError()</h1>
- <dl>
- <dt>should return an Error</dt>
- <dd><pre><code>request
- .get('http://localhost:' + server.address().port)
- .end(function(err, res){
- var err = res.toError();
- assert.equal(err.status, 400);
- assert.equal(err.method, 'GET');
- assert.equal(err.path, '/');
- assert.equal(err.message, 'cannot GET / (400)');
- assert.equal(err.text, 'invalid json');
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>req.get()</h1>
- <dl>
- <dt>should set a default user-agent</dt>
- <dd><pre><code>request
- .get('http://localhost:3345/ua')
- .end(function(err, res){
- assert(res.headers);
- assert(res.headers['user-agent']);
- assert(/^node-superagent\/\d+\.\d+\.\d+$/.test(res.headers['user-agent']));
- done();
- });</code></pre></dd>
- <dt>should be able to override user-agent</dt>
- <dd><pre><code>request
- .get('http://localhost:3345/ua')
- .set('User-Agent', 'foo/bar')
- .end(function(err, res){
- assert(res.headers);
- assert.equal(res.headers['user-agent'], 'foo/bar');
- done();
- });</code></pre></dd>
- <dt>should be able to wipe user-agent</dt>
- <dd><pre><code>request
- .get('http://localhost:3345/ua')
- .unset('User-Agent')
- .end(function(err, res){
- assert(res.headers);
- assert.equal(res.headers['user-agent'], void 0);
- done();
- });</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>utils.type(str)</h1>
- <dl>
- <dt>should return the mime type</dt>
- <dd><pre><code>utils.type('application/json; charset=utf-8')
- .should.equal('application/json');
- utils.type('application/json')
- .should.equal('application/json');</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>utils.params(str)</h1>
- <dl>
- <dt>should return the field parameters</dt>
- <dd><pre><code>var str = 'application/json; charset=utf-8; foo = bar';
- var obj = utils.params(str);
- obj.charset.should.equal('utf-8');
- obj.foo.should.equal('bar');
- var str = 'application/json';
- utils.params(str).should.eql({});</code></pre></dd>
- </dl>
- </section>
- <section class="suite">
- <h1>utils.parseLinks(str)</h1>
- <dl>
- <dt>should parse links</dt>
- <dd><pre><code>var str = '<https://api.github.com/repos/visionmedia/mocha/issues?page=2>; rel="next", <https://api.github.com/repos/visionmedia/mocha/issues?page=5>; rel="last"';
- var ret = utils.parseLinks(str);
- ret.next.should.equal('https://api.github.com/repos/visionmedia/mocha/issues?page=2');
- ret.last.should.equal('https://api.github.com/repos/visionmedia/mocha/issues?page=5');</code></pre></dd>
- </dl>
- </section>
- </div>
- <a href="http://github.com/visionmedia/superagent"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
- </body>
- </html>
|