import { expect } from "chai" ; import "mocha" ; import { Headers , RequestInit , Response , ResponseInit , } from "node-fetch" ; import { HttpClient , IHttpClientOptions , IProxy , IRequestContext } from "../httpClient" ; import RequestResponseLog from "../requestResponseLog" ; describe ( "20-NEXCLOUD-NODE-CLIENT-HTTP-CLIENT" , function ( ) { this . timeout ( 1 * 60 * 1000 ) ; it ( "01 timeout unkonwn server" , async ( ) => { const proxy = { host : "host" , port : "1234" , protocol : "https" , proxyAuthorizationHeader : "some header" , secureProxy : true , } ; const options = { authorizationHeader : "basic 12343" , logRequestResponse : true , origin : "origin" , proxy , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { method : "GET" , timeout : 1 , } ; const url = "https://this.server.does.not.exist:1234/root" ; let response ; let exceptionOccurred = false ; try { response = await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { description : "test Call" } ) ; } catch ( e ) { exceptionOccurred = true ; } expect ( exceptionOccurred , "expect an exception" ) . to . be . equal ( true ) ; } ) ; it ( "02 post with basic auth" , async ( ) => { const options = { authorizationHeader : "Basic dGVzdDp0ZXN0" , logRequestResponse : false , origin : "https://ptsv2.com" , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { headers : new Headers ( ) , method : "POST" , } ; const url = "https://ptsv2.com/t/jb6te-1577116991/post" ; let response ; try { response = await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { description : "test Call" } ) ; const body = await response . text ( ) ; expect ( body ) . to . be . equal ( "Thank you for this dump. I hope you have a lovely day!" ) ; } catch ( e ) { expect ( true , "expect no exception" ) . to . be . equal ( e . message ) ; } } ) ; it ( "03 post without credentials" , async ( ) => { const options = { logRequestResponse : false , origin : "https://ptsv2.com" , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { method : "POST" , } ; const url = "https://ptsv2.com/t/jb6te-1577116991/post" ; let exceptionMessage = "" ; try { await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { description : "test Call" } ) ; } catch ( e ) { expect ( true , "expect an exception" ) . to . be . equal ( true ) ; exceptionMessage = e . message ; } expect ( exceptionMessage , "expect an exception" ) . to . be . not . equal ( "" ) ; } ) ; it ( "04 post without credentials recording mode" , async ( ) => { const rrLog = RequestResponseLog . getInstance ( ) ; await rrLog . setContext ( "04 post without credentials recording mode" ) ; const options = { logRequestResponse : true , origin : "https://ptsv2.com" , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { method : "POST" , } ; const url = "https://ptsv2.com/t/rdztk-1577118815/post" ; let response ; try { response = await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { description : "test Call" } ) ; const body = await response . text ( ) ; expect ( body ) . to . be . equal ( `{"test":true}` ) ; const body2 = await response . json ( ) ; expect ( body2 ) . to . be . have . property ( "test" ) ; expect ( body2 . test ) . to . be . equal ( true ) ; const body3 = await response . buffer ( ) ; expect ( body3 ) . to . be . not . equal ( null ) ; } catch ( e ) { expect ( true , "expect no exception" ) . to . be . equal ( e . message ) ; } } ) ; it ( "05 post only absolute urls are supported" , async ( ) => { const rrLog = RequestResponseLog . getInstance ( ) ; await rrLog . setContext ( "04 post without credentials recording mode" ) ; const options = { logRequestResponse : true , origin : "https://ptsv2.com" , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { method : "POST" , } ; const url = "/t/rdztk-1577118815/post" ; try { await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { description : "test Call" } ) ; } catch ( e ) { expect ( true , "expect an exception" ) . to . be . equal ( true ) ; } } ) ; it ( "06 without proxy auth header, origin, description and method" , async ( ) => { const proxy = { host : "host" , port : "1234" , protocol : "https" , secureProxy : true , } ; const options = { authorizationHeader : "basic 12343" , logRequestResponse : true , proxy , } ; const httpClient = new HttpClient ( options ) ; const requestInit = { timeout : 1 , } ; const url = "https://this.server.does.not.exist:1234/root" ; let response ; let exceptionOccurred = false ; try { response = await httpClient . getHttpResponse ( url , requestInit , [ 200 ] , { } ) ; } catch ( e ) { exceptionOccurred = true ; } expect ( exceptionOccurred ) . to . be . equal ( true ) ; } ) ; } ) ;