Skip to content

Commit

Permalink
fix: migrate from tap to node test and c8 (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dancastillo authored Aug 7, 2024
1 parent 4f8f074 commit aad27ca
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
3 changes: 0 additions & 3 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"devDependencies": {
"@types/node": "^22.0.0",
"benchmark": "2.1.4",
"c8": "^10.1.2",
"standard": "^17.1.0",
"tap": "^18.8.0",
"tsd": "^0.31.1"
},
"types": "types/index.d.ts",
Expand All @@ -42,7 +42,7 @@
"lint": "standard",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "c8 node --test",
"test:typescript": "tsd"
}
}
5 changes: 0 additions & 5 deletions test/.eslintrc

This file was deleted.

52 changes: 40 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,117 @@
'use strict'

const { test } = require('tap')
const test = require('node:test')
const forwarded = require('..')

test('should require req', function (t) {
t.plan(1)
t.throws(forwarded.bind(null), 'argument req.*required')
t.assert.throws(forwarded.bind(null), 'argument req.*required')
})

test('should work with X-Forwarded-For header', function (t) {
t.plan(1)
const req = createReq('127.0.0.1')
t.same(forwarded(req), ['127.0.0.1'])
t.assert.deepStrictEqual(forwarded(req), ['127.0.0.1'])
})

test('should include entries from X-Forwarded-For', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.2, 10.0.0.1'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should include entries from X-Forwarded-For', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': ' '
})
t.same(forwarded(req), ['127.0.0.1'])
t.assert.deepStrictEqual(forwarded(req), ['127.0.0.1'])
})

test('should include entries from X-Forwarded-For', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1'])
t.assert.deepStrictEqual(forwarded(req), ['127.0.0.1', '10.0.0.1'])
})

test('should skip blank entries', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.2,, 10.0.0.1'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should trim leading OWS', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': ' 10.0.0.2 , , 10.0.0.1 '
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should handle correctly when beginning with a comma', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': ', 10.0.0.2 , , 10.0.0.1'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should handle correctly when ending with a comma', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.2 , , 10.0.0.1,'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should trim trailing OWS before a comma', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': ' , 10.0.0.2 , , 10.0.0.1'
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

test('should trim trailing OWS after a comma', function (t) {
t.plan(1)
const req = createReq('127.0.0.1', {
'x-forwarded-for': ' 10.0.0.2 , , 10.0.0.1 , '
})
t.same(forwarded(req), ['127.0.0.1', '10.0.0.1', '10.0.0.2'])
t.assert.deepStrictEqual(forwarded(req), [
'127.0.0.1',
'10.0.0.1',
'10.0.0.2'
])
})

function createReq (socketAddr, headers) {
Expand Down

0 comments on commit aad27ca

Please sign in to comment.