import assert from 'assert' ; import { describe , it } from 'mocha' ; import { request } from '../src/index' ; import * as uuid from 'uuid' ; const port = 7172 ; describe ( '💻 browser tests' , ( ) => { it ( 'should just work from browser' , async ( ) => { const result = await request ( { url : `http://localhost:${ port }/path` } ) ; assert . strictEqual ( result . status , 200 ) ; assert . strictEqual ( result . data , 'response' ) ; } ) ; it ( 'should pass querystring parameters from browser' , async ( ) => { const result = await request ( { url : `http://localhost:${ port }/querystring` , params : { query : 'value' } , } ) ; assert . strictEqual ( result . status , 200 ) ; assert . strictEqual ( result . data , 'value' ) ; } ) ; it ( 'should allow passing browser fetch explicitly' , async ( ) => { const result = await request ( { url : `http://localhost:${ port }/path` , fetchImplementation : window . fetch , } ) ; assert . strictEqual ( result . status , 200 ) ; } ) ; it ( 'should support multipart post from the browser' , async ( ) => { const headers = { } ; const multipart = [ { 'Content-Type' : 'application/json' , body : JSON . stringify ( { hello : 'world' } ) , } , { 'Content-Type' : 'text/plain' , body : 'hello world!' , } , ] ; const boundary = uuid . v4 ( ) ; const finale = `--${ boundary }--` ; headers [ 'Content-Type' ] = `multipart/related; boundary=${ boundary }` ; let content = '' ; for ( const part of multipart ) { const preamble = `--${ boundary }\r\nContent-Type: ${ part [ 'Content-Type' ] }\r\n\r\n` ; content += preamble ; if ( typeof part . body === 'string' ) { content += part . body ; content += '\r\n' ; } } content += finale ; const result = await request ( { headers , data : content , method : 'POST' , url : `http://localhost:${ port }/path` , } ) ; assert . strictEqual ( result . status , 200 ) ; const parts = result . data as string [ ] ; assert . strictEqual ( parts [ 0 ] , '{"hello":"world"}' ) ; assert . strictEqual ( parts [ 1 ] , 'hello world!' ) ; } ) ; } ) ;