@@ -13,13 +13,24 @@ import { HttpClient } from "../../src/httpClient";
13
13
import { getResponse } from "../testUtils" ;
14
14
import { createMockEntityFromDiscriminatorValue } from "./mockEntity" ;
15
15
import { MockParseNode , MockParseNodeFactory } from "./mockParseNodeFactory" ;
16
+ import { JsonParseNode , JsonParseNodeFactory } from "@microsoft/kiota-serialization-json" ;
17
+ import { TextParseNodeFactory } from "@microsoft/kiota-serialization-text" ;
18
+ import { FormParseNodeFactory } from "@microsoft/kiota-serialization-form" ;
16
19
17
20
// eslint-disable-next-line no-var
18
21
var Response = Response ;
19
22
if ( typeof Response !== "object" ) {
20
23
Response = getResponse ( ) ;
21
24
}
22
25
26
+ const TestEnumObject = {
27
+ A : "a" ,
28
+ B : "b" ,
29
+ C : "c"
30
+ } as const ;
31
+
32
+ type TestEnum = ( typeof TestEnumObject ) [ keyof typeof TestEnumObject ] ;
33
+
23
34
describe ( "FetchRequestAdapter.ts" , ( ) => {
24
35
describe ( "getClaimsFromResponse" , ( ) => {
25
36
it ( "should get claims from response header" , async ( ) => {
@@ -124,6 +135,55 @@ describe("FetchRequestAdapter.ts", () => {
124
135
} ) ;
125
136
}
126
137
} ) ;
138
+ describe ( "send enum" , ( ) => {
139
+ for ( const statusCode of [ 200 , 201 , 202 , 203 ] ) {
140
+ const enumResponse = "a" ;
141
+ it ( `should return object for status code ${ statusCode } ` , async ( ) => {
142
+ const mockHttpClient = new HttpClient ( ) ;
143
+ mockHttpClient . executeFetch = async ( url : string , requestInit : RequestInit , requestOptions ?: Record < string , RequestOption > ) => {
144
+ const response = new Response ( enumResponse , {
145
+ status : statusCode ,
146
+ } as ResponseInit ) ;
147
+ response . headers . set ( "Content-Type" , "text/plain" ) ;
148
+ return Promise . resolve ( response ) ;
149
+ } ;
150
+ const mockFactory = new TextParseNodeFactory ( ) ; //new JsonParseNodeFactory();
151
+ const requestAdapter = new FetchRequestAdapter ( new AnonymousAuthenticationProvider ( ) , mockFactory , undefined , mockHttpClient ) ;
152
+ const requestInformation = new RequestInformation ( ) ;
153
+ requestInformation . URL = "https://www.example.com" ;
154
+ requestInformation . httpMethod = HttpMethod . GET ;
155
+ const result = await requestAdapter . sendEnum ( requestInformation , TestEnumObject , undefined ) ;
156
+ assert . isDefined ( result ) ;
157
+ assert . equal ( result , TestEnumObject . A ) ;
158
+ } ) ;
159
+ }
160
+ } ) ;
161
+ describe ( "send and deserialize collection of enum" , ( ) => {
162
+ for ( const statusCode of [ 200 , 201 , 202 , 203 ] ) {
163
+ const enumResponse = `["a","b","c","e","f"]` ;
164
+ it ( `should return object for status code ${ statusCode } ` , async ( ) => {
165
+ const mockHttpClient = new HttpClient ( ) ;
166
+ mockHttpClient . executeFetch = async ( url : string , requestInit : RequestInit , requestOptions ?: Record < string , RequestOption > ) => {
167
+ const response = new Response ( enumResponse , {
168
+ status : statusCode ,
169
+ } as ResponseInit ) ;
170
+ response . headers . set ( "Content-Type" , "application/json" ) ;
171
+ return Promise . resolve ( response ) ;
172
+ } ;
173
+ const mockFactory = new JsonParseNodeFactory ( ) ;
174
+ const requestAdapter = new FetchRequestAdapter ( new AnonymousAuthenticationProvider ( ) , mockFactory , undefined , mockHttpClient ) ;
175
+ const requestInformation = new RequestInformation ( ) ;
176
+ requestInformation . URL = "https://www.example.com" ;
177
+ requestInformation . httpMethod = HttpMethod . GET ;
178
+ const result = await requestAdapter . sendCollectionOfEnum ( requestInformation , TestEnumObject , undefined ) ;
179
+ assert . isDefined ( result ) ;
180
+ assert . equal ( result ?. length , 3 ) ;
181
+ assert . equal ( result ! [ 0 ] , TestEnumObject . A ) ;
182
+ assert . equal ( result ! [ 1 ] , TestEnumObject . B ) ;
183
+ assert . equal ( result ! [ 2 ] , TestEnumObject . C ) ;
184
+ } ) ;
185
+ }
186
+ } ) ;
127
187
describe ( "Throws API error" , ( ) => {
128
188
it ( "should throw API error" , async ( ) => {
129
189
const mockHttpClient = new HttpClient ( ) ;
0 commit comments