1
1
import { MetadataScanner } from "@nestjs/core" ;
2
2
import { beforeEach , describe , test , expect } from "vitest" ;
3
- import { GetExchange , HttpInterface , PathVariable } from "./decorators" ;
3
+ import {
4
+ GetExchange ,
5
+ HttpInterface ,
6
+ PathVariable ,
7
+ RequestParam ,
8
+ } from "./decorators" ;
4
9
import { StubDiscoveryService } from "./fixture/stub-discovery.service" ;
5
10
import { StubHttpClient } from "./fixture/stub-http-client" ;
6
11
import { NodeFetchInjector } from "./node-fetch.injector" ;
@@ -53,10 +58,9 @@ describe("NodeFetchInjector", () => {
53
58
nodeFetchInjector . onModuleInit ( ) ;
54
59
55
60
// when
56
- const actual = await instance . request ( ) ;
61
+ await instance . request ( ) ;
57
62
58
63
// then
59
- expect ( actual ) . toEqual ( { status : "ok" } ) ;
60
64
expect ( httpClient . requestInfo ) . toHaveLength ( 1 ) ;
61
65
expect ( httpClient . requestInfo [ 0 ] . url ) . toBe ( "https://example.com/api" ) ;
62
66
} ) ;
@@ -75,10 +79,9 @@ describe("NodeFetchInjector", () => {
75
79
nodeFetchInjector . onModuleInit ( ) ;
76
80
77
81
// when
78
- const actual = await instance . request ( "1" ) ;
82
+ await instance . request ( "1" ) ;
79
83
80
84
// then
81
- expect ( actual ) . toEqual ( { status : "ok" } ) ;
82
85
expect ( httpClient . requestInfo ) . toHaveLength ( 1 ) ;
83
86
expect ( httpClient . requestInfo [ 0 ] . url ) . toBe (
84
87
"https://example.com/api/users/1/1"
@@ -102,13 +105,60 @@ describe("NodeFetchInjector", () => {
102
105
nodeFetchInjector . onModuleInit ( ) ;
103
106
104
107
// when
105
- const actual = await instance . request ( "123" , "active" ) ;
108
+ await instance . request ( "123" , "active" ) ;
106
109
107
110
// then
108
- expect ( actual ) . toEqual ( { status : "ok" } ) ;
109
111
expect ( httpClient . requestInfo ) . toHaveLength ( 1 ) ;
110
112
expect ( httpClient . requestInfo [ 0 ] . url ) . toBe (
111
113
"https://example.com/api/users/123/active"
112
114
) ;
113
115
} ) ;
116
+
117
+ test ( "should append query sting to url" , async ( ) => {
118
+ // given
119
+ @HttpInterface ( )
120
+ class SampleClient {
121
+ @GetExchange ( "https://example.com/api" )
122
+ async request ( @RequestParam ( "keyword" ) keyword : string ) : Promise < string > {
123
+ return "request" ;
124
+ }
125
+ }
126
+ const instance = discoveryService . addProvider ( SampleClient ) ;
127
+ httpClient . addResponse ( { status : "ok" } ) ;
128
+ nodeFetchInjector . onModuleInit ( ) ;
129
+
130
+ // when
131
+ await instance . request ( "search" ) ;
132
+
133
+ // then
134
+ expect ( httpClient . requestInfo ) . toHaveLength ( 1 ) ;
135
+ expect ( httpClient . requestInfo [ 0 ] . url ) . toBe (
136
+ "https://example.com/api?keyword=search"
137
+ ) ;
138
+ } ) ;
139
+
140
+ test ( "should append query sting to url when provided as json" , async ( ) => {
141
+ // given
142
+ @HttpInterface ( )
143
+ class SampleClient {
144
+ @GetExchange ( "https://example.com/api" )
145
+ async request (
146
+ @RequestParam ( ) params : Record < string , unknown >
147
+ ) : Promise < string > {
148
+ return "request" ;
149
+ }
150
+ }
151
+ const instance = discoveryService . addProvider ( SampleClient ) ;
152
+ httpClient . addResponse ( { status : "ok" } ) ;
153
+ nodeFetchInjector . onModuleInit ( ) ;
154
+
155
+ // when
156
+ await instance . request ( { keyword : "search" , page : 1 , isActive : true } ) ;
157
+
158
+ // then
159
+ expect ( httpClient . requestInfo ) . toHaveLength ( 1 ) ;
160
+ expect ( httpClient . requestInfo [ 0 ] . url ) . toBe (
161
+ "https://example.com/api?keyword=search&page=1&isActive=true"
162
+ ) ;
163
+ } ) ;
114
164
} ) ;
0 commit comments