Comments (18)
[Original: +1, is there a recommended way of testing sessions without this functionality?]
After inspecting the code I realized this functionality is already built into supertest (just not very well documented).
Just do:
var supertest = require('supertest');
var app = require('../lib/your_app_location');
var request = supertest(app);
var agent = supertest.agent(app);
Works like a charm, this issue should be closed, and so should be #46
from supertest.
I was just about to request this too. I'm using Supertest to test our website and Ajax APIs, and was really looking forward to the automatic cookie/session support in the update to Supertest/Superagent. Thanks!
from supertest.
A workaround is that you can require both supertest and superagent and use superagent to get the agent for the tests that need sessions and cookies. But you also have to then fully qualify your urls. This addition would be a nice convenience.
from supertest.
How do you hook up the Superagent agent to the Supertest functionality?
from supertest.
I didn't. Obviously you could, and that's what I'm suggesting in this issue. For now, I was just using both of them
var request = require('supertest')
, agent = require('superagent').agent
, express = require('express')
, app = require('../app');
...
// supertest
describe('GET /admin/project', function(){
describe('when the user is not looged in as admin', function(){
it('should return to the admin home page', function(done){
request(app)
.get('/admin/project')
...
// superagent
describe('when an admin is logged in', function(){
it('should show the list of projects', function(done){
var user = agent();
user
.post('http://localhost:3000/admin')
from supertest.
+1
from supertest.
I'd love too see that feature implemented, even wanted to do this myself, but unfortunatelly superagent's agent implementation doesn't fit well in current supertest design :(
from supertest.
Perhaps supertest could expose a similar agent API as superagent?
var request = require('supertest').agent();
request(app)
.get('/login')
// ...
request(app)
.get('/admin')
// ...
from supertest.
I made it working although in not prettiest way (#32). In my fork you can use supertest right now like that:
request(app).agent()
to access superagent's agent.
from supertest.
Finally I've wrote separate module (https://github.com/codefather/supertest-chai) which is not based but very similar to supertest.
It doesn't have sexy supertest's expect API, but relies on chai.js (with some customs assertions) to do the work.
Superagent's agent works with no problems.
Example:
var chaiSupertest = require('chai-supertest');
var request = chaiSupertest.request;
var chai = require("chai");
chai.should();
chai.use(chaiSupertest.httpAsserts);
var express = require('express');
var app = express();
app.get('/user', function (req, res) {
res.send(201, { name: 'tobi' });
});
app.get('/questions', function (req, res) {
res.send(200, 'test');
});
request(app)
.get('/user')
.end(function (res) {
res.should.be.json;
res.should.have.status(201);
res.should.have.header('Content-Length', '15');
res.body.should.deep.equal({name: 'tobi'});
});
var user = request(app).agent();
user
.get('/questions')
.end(function (res) {
res.should.be.html;
res.should.have.status(200);
res.should.have.header('Content-Length', '4');
res.text.should.equal('test');
});
from supertest.
+1 - this'd be really nice to have.
from supertest.
+1
from supertest.
+1
from supertest.
+1
from supertest.
+1, unusable without it.
from supertest.
+1
from supertest.
+1
from supertest.
Just need to add docs for this.
from supertest.
Related Issues (20)
- Notice regarding the move from @visionmedia org to @ladjs org
- Cookies go to domains they didn't return from
- disabling the debug REQURL CODE line that comes while testing HOT 1
- HTTP2 sample no working
- [fix] TypeError: Cannot read properties of undefined (reading 'address') HOT 1
- cookiejar Regular Expression Denial of Service via Cookie.parse function HOT 3
- `_body` key instead of `body` in response HOT 1
- Its is a good practice for Next.js?
- [fix] timeout doesn't call end with error
- [fix] Module not found: Error: Can't resolve 'http' in '[...]/node_modules/supertest/lib' HOT 1
- [fix] Do no replace invalid body with an empty JSON object HOT 1
- I didnt find supertest latest version updated with latest superagent 8.0.9 HOT 3
- Why I was getting "Jest has detected the following 1 open handle potentially keeping Jest from exiting: Timeout"
- Error: write EPIPE on large files - Next.js + Jest + Supertest HOT 1
- [fix] Cannot send secure cookie over unencrypted connection
- [fix] Next.js: Ubuntu 22.04.3 LTS
- [fix] don't change the requrest url hash HOT 1
- SuperTest is not compatible with Node v18.19.0 Error: ECONNREFUSED: Connection Refused HOT 3
- [fix] write EPIPE error when attaching a file. (small repro)
- supertest version 6.3.3 => 6.3.4 is a breaking change HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from supertest.