Skip to content

Commit 0036afc

Browse files
committed
fix(HttpAddress): Add path to HttpAddress
1 parent 04f1412 commit 0036afc

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

src/base/Transports.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
1-
import { TcpAddress } from './Transports'
1+
import { TcpAddress, HttpAddress } from './Transports'
22

3-
describe('tcpAddress', () => {
3+
describe('TcpAddress', () => {
44
const defaults = {
55
host: '127.0.0.1',
66
port: 4321
77
}
88

9-
test('parses a string with host and port', () => {
9+
test('constructor: string with host and port', () => {
1010
expect(new TcpAddress('example.com:2010', defaults)).toEqual({
1111
type: 'tcp',
1212
host: 'example.com',
1313
port: 2010
1414
})
1515
})
1616

17-
test('parses a number as the port', () => {
17+
test('constructor: number as the port', () => {
1818
expect(new TcpAddress(2020, defaults)).toEqual({
1919
type: 'tcp',
2020
host: '127.0.0.1',
2121
port: 2020
2222
})
2323
})
2424

25-
test('parses a string with just port', () => {
25+
test('constructor: string with just port', () => {
2626
expect(new TcpAddress('2030', defaults)).toEqual({
2727
type: 'tcp',
2828
host: '127.0.0.1',
2929
port: 2030
3030
})
3131
})
32+
33+
test('toString', () => {
34+
expect(new TcpAddress().toString()).toEqual('tcp://127.0.0.1:2000')
35+
})
36+
})
37+
38+
describe('HttpAddress', () => {
39+
test('constructor: string with host and port', () => {
40+
expect(new HttpAddress()).toEqual({
41+
type: 'http',
42+
host: '127.0.0.1',
43+
port: 8000,
44+
path: '',
45+
jwt: undefined
46+
})
47+
})
48+
49+
test('toString', () => {
50+
expect(new HttpAddress().toString()).toEqual('http://127.0.0.1:8000')
51+
52+
expect(new HttpAddress(undefined, '/some/path').toString()).toEqual(
53+
'http://127.0.0.1:8000/some/path'
54+
)
55+
})
3256
})

src/base/Transports.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class UdsAddress {
3939
}
4040

4141
/**
42-
* An address int the Linux VSOCK address family
42+
* An address in the Linux VSOCK address family
4343
*
4444
* @see http://man7.org/linux/man-pages/man7/vsock.7.html
4545
*/
@@ -123,18 +123,43 @@ export class TcpAddress {
123123
}
124124
}
125125

126+
/**
127+
* An address in the HTTP address family
128+
*
129+
* @see https://www.w3.org/Addressing/HTTPAddressing.html
130+
*/
126131
export class HttpAddress extends TcpAddress {
127132
public readonly type: Transport.http | Transport.ws = Transport.http
128133

134+
/**
135+
* The path for the address.
136+
*
137+
* Should begin with a forward slash e.g. `/some/route`
138+
*/
139+
public readonly path: string
140+
141+
/**
142+
* The JSON Web Token (JWT) to use add to requests
143+
* to this address.
144+
*/
129145
public readonly jwt?: string
130146

131-
public constructor(address?: TcpAddressInitializer, jwt?: string) {
147+
public constructor(
148+
address?: TcpAddressInitializer,
149+
path: string = '',
150+
jwt?: string
151+
) {
132152
super(address, {
133153
host: '127.0.0.1',
134154
port: 8000
135155
})
156+
this.path = path
136157
this.jwt = jwt
137158
}
159+
160+
public toString(): string {
161+
return `${super.toString()}${this.path}`
162+
}
138163
}
139164

140165
export class WebSocketAddress extends HttpAddress {

src/http/HttpServer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default class HttpServer extends TcpServer {
100100
host: this.host,
101101
port: this.port
102102
},
103+
'',
103104
this.defaultJwt
104105
)
105106
}

0 commit comments

Comments
 (0)