-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcorrelation.e2e-spec.ts
65 lines (55 loc) · 1.77 KB
/
correlation.e2e-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Test, TestingModule } from '@nestjs/testing';
import {
HttpStatus,
INestApplication,
MiddlewareConsumer,
Module,
NestModule,
} from '@nestjs/common';
import * as request from 'supertest';
import { CorrelationIdMiddleware, CorrelationModule } from '../src';
import { TestModule } from './test-module/test.module';
import { HttpService } from '@nestjs/axios';
import { of } from 'rxjs';
import { AxiosRequestHeaders } from 'axios';
@Module({
imports: [CorrelationModule.forRoot(), TestModule],
})
class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(CorrelationIdMiddleware).forRoutes('*');
}
}
describe('CorrelationMiddleware (e2e)', () => {
let app: INestApplication;
let httpService: HttpService;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
httpService = moduleFixture.get<HttpService>(HttpService);
app = moduleFixture.createNestApplication();
await app.init();
jest.spyOn(httpService, 'get').mockImplementation(() =>
of({
config: { url: 'http://example.com/test', method: 'GET', headers: {} as AxiosRequestHeaders },
headers: {
connection: 'keep-alive',
'content-type': 'application/json',
},
status: HttpStatus.OK,
statusText: 'OK',
data: { foo: 'bar' },
}),
);
});
it('/ (GET)', () => {
return request(app.getHttpServer()).get('/').expect(200).expect('pong');
});
it('/ (GET) with correlation id', async () => {
const result = await request(app.getHttpServer())
.get('/')
.set('X-Correlation-Id', 'test-id-1');
expect(result.headers['x-correlation-id']).toBe('test-id-1');
});
});