11/* ©️ 2016 - present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com */
22
3- import * as request from 'fc-node-requests' ;
4-
53import { HttpAuthErr , HttpClientErr } from '../lib/api' ;
64
75import { BackendData } from './backend-data' ;
@@ -14,30 +12,6 @@ import { expect } from 'chai';
1412
1513export const mockBackendData = new BackendData ( oauth ) ;
1614
17- const fwdToRealBackend = async ( parsed : any , req : IncomingMessage ) : Promise < string > => {
18- // we are forwarding this request to backend, but we are not properly authenticated with real backend
19- // better remove authentication: requests that we currently forward during tests don't actually require it
20- delete req . headers . host ;
21- delete req . headers [ 'content-length' ] ;
22- const forwarding : any = { headers : req . headers , url : `https://flowcrypt.com${ req . url } ` } ;
23- if ( req . url ! . includes ( 'message/upload' ) ) {
24- // Removing mock auth when forwarding request to real backend at ${req.url}
25- // here a bit more difficult, because the request was already encoded as form-data
26- parsed . body = ( parsed . body as string ) . replace ( / ( - - - - - E N D P G P M E S S A G E - - - - - \r \n \r \n - - - - - - [ A - Z a - z 0 - 9 ] + ) ( .| \r \n ) * $ / gm, ( _ , found ) => `${ found } --\r\n` ) ;
27- forwarding . body = parsed . body ; // FORM-DATA
28- const r = await request . post ( forwarding ) ;
29- return r . body ; // already json-stringified for this call, maybe because backend doesn't return proper content-type
30- }
31- if ( parsed . body && typeof parsed . body === 'object' && parsed . body . account && parsed . body . uuid ) {
32- // Removing mock auth when forwarding request to real backend at ${req.url}
33- delete parsed . body . account ;
34- delete parsed . body . uuid ;
35- }
36- forwarding . json = parsed . body ; // JSON
37- const r = await request . post ( forwarding ) ;
38- return JSON . stringify ( r . body ) ;
39- } ;
40-
4115export const mockBackendEndpoints : HandlersDefinition = {
4216 '/api/account/login' : async ( { body } , req ) => {
4317 const parsed = throwIfNotPostWithAuth ( body , req ) ;
@@ -78,11 +52,15 @@ export const mockBackendEndpoints: HandlersDefinition = {
7852 expect ( ( body as any ) . email ) . to . equal ( 'flowcrypt.compatibility@gmail.com' ) ;
7953 return { sent : true , text : 'Feedback sent' } ;
8054 } ,
81- '/api/message/presign_files' : fwdToRealBackend ,
82- '/api/message/confirm_files' : fwdToRealBackend ,
83- '/api/message/upload' : fwdToRealBackend ,
84- '/api/link/message' : fwdToRealBackend ,
85- '/api/link/me' : fwdToRealBackend ,
55+ '/api/message/upload' : async ( { } ) => {
56+ return { short : '0000000000' , url : 'https://example.com/msg-123' , admin_code : 'mocked-admin-code' } ;
57+ } ,
58+ '/api/link/message' : async ( { } ) => {
59+ return { "url" : "https://example.com/msg-123" , "repliable" : "False" , "expire" : "2100-05-18 16:31:28" , "expired" : "False" , "deleted" : "False" } ;
60+ } ,
61+ '/api/link/me' : async ( { } , req ) => {
62+ throw new Error ( `${ req . url } mock not implemented` ) ;
63+ } ,
8664} ;
8765
8866const throwIfNotPostWithAuth = ( body : unknown , req : IncomingMessage ) => {
0 commit comments