@@ -25,7 +25,7 @@ describe('OFREPApi', () => {
2525 } ) ;
2626 beforeEach ( ( ) => {
2727 jest . useFakeTimers ( ) ;
28- api = new OFREPApi ( 'https://localhost:8080' ) ;
28+ api = new OFREPApi ( { baseUrl : 'https://localhost:8080' } ) ;
2929 } ) ;
3030 afterEach ( ( ) => {
3131 jest . runOnlyPendingTimers ( ) ;
@@ -38,31 +38,31 @@ describe('OFREPApi', () => {
3838
3939 describe ( 'postEvaluateFlags should' , ( ) => {
4040 it ( 'throw OFREPApiFetchError on network error' , async ( ) => {
41- await expect ( ( ) => api . postEvaluateFlags ( 'my-flag' , { context : { errors : { network : true } } } ) ) . rejects . toThrow (
41+ await expect ( ( ) => api . postEvaluateFlag ( 'my-flag' , { context : { errors : { network : true } } } ) ) . rejects . toThrow (
4242 OFREPApiFetchError ,
4343 ) ;
4444 } ) ;
4545
4646 it ( 'throw OFREPApiUnexpectedResponseError on any error code without EvaluationFailureResponse body' , async ( ) => {
4747 await expect ( ( ) =>
48- api . postEvaluateFlags ( 'my-flag' , { context : { errors : { generic400 : true } } } ) ,
48+ api . postEvaluateFlag ( 'my-flag' , { context : { errors : { generic400 : true } } } ) ,
4949 ) . rejects . toThrow ( OFREPApiUnexpectedResponseError ) ;
5050 } ) ;
5151
5252 it ( 'throw OFREPForbiddenError on 401 response' , async ( ) => {
53- await expect ( ( ) => api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 401 : true } } } ) ) . rejects . toThrow (
53+ await expect ( ( ) => api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 401 : true } } } ) ) . rejects . toThrow (
5454 OFREPApiUnauthorizedError ,
5555 ) ;
5656 } ) ;
5757
5858 it ( 'throw OFREPForbiddenError on 403 response' , async ( ) => {
59- await expect ( ( ) => api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 403 : true } } } ) ) . rejects . toThrow (
59+ await expect ( ( ) => api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 403 : true } } } ) ) . rejects . toThrow (
6060 OFREPForbiddenError ,
6161 ) ;
6262 } ) ;
6363
6464 it ( 'throw OFREPApiTooManyRequestsError on 429 response' , async ( ) => {
65- await expect ( ( ) => api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 429 : true } } } ) ) . rejects . toThrow (
65+ await expect ( ( ) => api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 429 : true } } } ) ) . rejects . toThrow (
6666 OFREPApiTooManyRequestsError ,
6767 ) ;
6868 } ) ;
@@ -71,7 +71,7 @@ describe('OFREPApi', () => {
7171 jest . setSystemTime ( new Date ( '2018-01-27' ) ) ;
7272
7373 try {
74- await api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 429 : true } } } ) ;
74+ await api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 429 : true } } } ) ;
7575 } catch ( error ) {
7676 if ( ! ( error instanceof OFREPApiTooManyRequestsError ) ) {
7777 throw new Error ( 'Expected OFREPApiTooManyRequestsError' ) ;
@@ -86,7 +86,7 @@ describe('OFREPApi', () => {
8686 jest . setSystemTime ( new Date ( '2018-01-27' ) ) ;
8787
8888 try {
89- await api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 429 : 'Sat, 27 Jan 2018 07:28:00 GMT' } } } ) ;
89+ await api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 429 : 'Sat, 27 Jan 2018 07:28:00 GMT' } } } ) ;
9090 } catch ( error ) {
9191 if ( ! ( error instanceof OFREPApiTooManyRequestsError ) ) {
9292 throw new Error ( 'Expected OFREPApiTooManyRequestsError' ) ;
@@ -101,7 +101,7 @@ describe('OFREPApi', () => {
101101 jest . setSystemTime ( new Date ( '2018-01-27' ) ) ;
102102
103103 try {
104- await api . postEvaluateFlags ( 'my-flag' , { context : { errors : { 429 : 'abcdefg' } } } ) ;
104+ await api . postEvaluateFlag ( 'my-flag' , { context : { errors : { 429 : 'abcdefg' } } } ) ;
105105 } catch ( error ) {
106106 if ( ! ( error instanceof OFREPApiTooManyRequestsError ) ) {
107107 throw new Error ( 'Expected OFREPApiTooManyRequestsError' ) ;
@@ -113,12 +113,12 @@ describe('OFREPApi', () => {
113113 } ) ;
114114
115115 it ( 'send empty request body if context is not given' , async ( ) => {
116- const result = await api . postEvaluateFlags ( 'my-flag' ) ;
116+ const result = await api . postEvaluateFlag ( 'my-flag' ) ;
117117 expect ( result . httpStatus ) . toEqual ( 200 ) ;
118118 } ) ;
119119
120120 it ( 'send evaluation context in request body' , async ( ) => {
121- const result = await api . postEvaluateFlags ( 'context-in-metadata' , {
121+ const result = await api . postEvaluateFlag ( 'context-in-metadata' , {
122122 context : {
123123 targetingKey : 'user-1' ,
124124 key1 : 'value1' ,
@@ -138,12 +138,12 @@ describe('OFREPApi', () => {
138138 } ) ;
139139
140140 it ( 'return HTTP status in result' , async ( ) => {
141- const result = await api . postEvaluateFlags ( 'my-flag' ) ;
141+ const result = await api . postEvaluateFlag ( 'my-flag' ) ;
142142 expect ( result . httpStatus ) . toEqual ( 200 ) ;
143143 } ) ;
144144
145145 it ( 'return EvaluationFailureResponse response as value on HTTP 400' , async ( ) => {
146- const result = await api . postEvaluateFlags ( 'my-flag' , { context : { errors : { notFound : true } } } ) ;
146+ const result = await api . postEvaluateFlag ( 'my-flag' , { context : { errors : { notFound : true } } } ) ;
147147 if ( result . httpStatus !== 404 ) {
148148 throw new Error ( 'Received unexpected HTTP status' ) ;
149149 }
@@ -155,7 +155,7 @@ describe('OFREPApi', () => {
155155 } ) ;
156156
157157 it ( 'return EvaluationFailureResponse response as value on HTTP 400' , async ( ) => {
158- const result = await api . postEvaluateFlags ( 'my-flag' , { context : { errors : { notFound : true } } } ) ;
158+ const result = await api . postEvaluateFlag ( 'my-flag' , { context : { errors : { notFound : true } } } ) ;
159159 if ( result . httpStatus !== 404 ) {
160160 throw new Error ( 'Received unexpected HTTP status' ) ;
161161 }
@@ -167,7 +167,7 @@ describe('OFREPApi', () => {
167167 } ) ;
168168
169169 it ( 'determine value type based on HTTP status' , async ( ) => {
170- const result = await api . postEvaluateFlags ( 'my-flag' ) ;
170+ const result = await api . postEvaluateFlag ( 'my-flag' ) ;
171171 expect ( result . httpStatus ) . toEqual ( 200 ) ;
172172
173173 // This is to check if the value type is determined by http status code
@@ -179,7 +179,7 @@ describe('OFREPApi', () => {
179179 } ) ;
180180
181181 it ( 'return EvaluationSuccessResponse response as value on successful evaluation' , async ( ) => {
182- const result = await api . postEvaluateFlags ( 'my-flag' , { context : { targetingKey : 'user' } } ) ;
182+ const result = await api . postEvaluateFlag ( 'my-flag' , { context : { targetingKey : 'user' } } ) ;
183183 expect ( result . httpStatus ) . toEqual ( 200 ) ;
184184 expect ( result . value ) . toEqual ( {
185185 key : 'my-flag' ,
@@ -193,6 +193,23 @@ describe('OFREPApi', () => {
193193 } ,
194194 } satisfies EvaluationSuccessResponse ) ;
195195 } ) ;
196+
197+ it ( 'send query params with request' , async ( ) => {
198+ api = new OFREPApi ( { baseUrl : 'https://localhost:8080' , query : new URLSearchParams ( { scope : '123' } ) } ) ;
199+ const result = await api . postEvaluateFlag ( 'my-flag' , { context : { targetingKey : 'user' } } ) ;
200+ expect ( result . httpStatus ) . toEqual ( 200 ) ;
201+ expect ( result . value ) . toEqual ( {
202+ key : 'my-flag' ,
203+ reason : EvaluationSuccessReason . TargetingMatch ,
204+ value : true ,
205+ variant : 'scoped' ,
206+ metadata : {
207+ context : {
208+ targetingKey : 'user' ,
209+ } ,
210+ } ,
211+ } satisfies EvaluationSuccessResponse ) ;
212+ } ) ;
196213 } ) ;
197214
198215 describe ( 'postBulkEvaluateFlags should' , ( ) => {
@@ -302,11 +319,26 @@ describe('OFREPApi', () => {
302319 } ) ;
303320
304321 it ( 'return BulkEvaluationNotModified response as value on 304' , async ( ) => {
305- const result = await api . postBulkEvaluateFlags ( undefined , { headers : [ [ 'If-None-Match' , '1234' ] ] } ) ;
322+ api = new OFREPApi ( { baseUrl : 'https://localhost:8080' , headers : [ [ 'If-None-Match' , '1234' ] ] } ) ;
323+ const result = await api . postBulkEvaluateFlags ( undefined ) ;
306324 expect ( result . httpStatus ) . toEqual ( 304 ) ;
307325 expect ( result . value ) . not . toBeDefined ( ) ;
308326 } ) ;
309327
328+ it ( 'send query params with request' , async ( ) => {
329+ api = new OFREPApi ( { baseUrl : 'https://localhost:8080' , query : new URLSearchParams ( { scope : '123' } ) } ) ;
330+ const result = await api . postBulkEvaluateFlags ( ) ;
331+ expect ( result . httpStatus ) . toEqual ( 200 ) ;
332+ expect ( result . value ) . toEqual ( {
333+ flags : [
334+ {
335+ key : 'other-flag' ,
336+ value : true ,
337+ } ,
338+ ] ,
339+ } ) ;
340+ } ) ;
341+
310342 it ( 'return BulkEvaluationSuccessResponse response as value on successful evaluation' , async ( ) => {
311343 const result = await api . postBulkEvaluateFlags ( ) ;
312344 expect ( result . httpStatus ) . toEqual ( 200 ) ;
0 commit comments