Skip to content

Commit

Permalink
feat: expect header exists or not
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 21, 2024
1 parent f510493 commit a57ee88
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,58 @@ export class Test extends Request {
return this;
}

/**
* UnExpectations:
*
* .unexpectHeader('Content-Type')
* .unexpectHeader('Content-Type', fn)
*/
unexpectHeader(name: string, fn?: CallbackFunction) {
if (typeof fn === 'function') {
this.end(fn);
}

// header
if (typeof name === 'string') {
this._asserts.push(this._unexpectHeader.bind(this, name));
}
return this;
}

/**
* Expectations:
*
* .expectHeader('Content-Type')
* .expectHeader('Content-Type', fn)
*/
expectHeader(name: string, fn?: CallbackFunction) {
if (typeof fn === 'function') {
this.end(fn);
}

// header
if (typeof name === 'string') {
this._asserts.push(this._expectHeader.bind(this, name));
}
return this;
}

_unexpectHeader(name: string, res: Response) {
const actual = res.headers[name.toLowerCase()];
if (actual) {
return new AssertError('unexpected "' + name + '" header field, got "' + actual + '"',
name, actual,
);
}
}

_expectHeader(name: string, res: Response) {
const actual = res.headers[name.toLowerCase()];
if (!actual) {
return new AssertError('expected "' + name + '" header field', name, actual);
}
}

/**
* Defer invoking superagent's `.end()` until
* the server is listening.
Expand Down
61 changes: 61 additions & 0 deletions test/supertest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,67 @@ describe('request(app)', function() {
});
});

describe('.expectHeader(name, fn)', function() {
it('should expect header exists', function(done) {
const app = express();

app.get('/', function(_req, res) {
res.setHeader('Foo-Bar', 'ok');
res.send('hey');
});

request(app)
.get('/')
.expect(200)
.expectHeader('Foo-Bar')
.expectHeader('content-type')
.end(done);
});

it('should expect header exists with callback', function(done) {
const app = express();

app.get('/', function(_req, res) {
res.setHeader('Foo-Bar', 'ok');
res.send('hey');
});

request(app)
.get('/')
.expect(200)
.expectHeader('Foo-Bar', done);
});
});

describe('.unexpectHeader(name, fn)', function() {
it('should expect header not exists', function(done) {
const app = express();

app.get('/', function(_req, res) {
res.send('hey');
});

request(app)
.get('/')
.expect(200)
.unexpectHeader('Foo-Bar')
.end(done);
});

it('should expect header not exists with callback', function(done) {
const app = express();

app.get('/', function(_req, res) {
res.send('hey');
});

request(app)
.get('/')
.expect(200)
.unexpectHeader('Foo-Bar', done);
});
});

describe('.expect(status[, fn])', function() {
it('should assert the response status', function(done) {
const app = express();
Expand Down

0 comments on commit a57ee88

Please sign in to comment.