@@ -17,46 +17,68 @@ class BaseApi {
1717 this . request . set ( this . headers ) ;
1818 }
1919
20+ setAllureAttachment ( res ) {
21+ // TODO: Handle other types that allure report can show properly.
22+ if ( Object . keys ( res . body ) . length !== 0 ) {
23+ const contentType = res . headers [ 'content-type' ]
24+ . includes ( 'application/json' ) ? 'application/json' : 'txt' ;
25+ const obj = contentType === 'application/json' ? JSON . stringify ( res . body ) : res . body ;
26+ reporter . addAttachment ( res . req . path , obj , contentType ) ;
27+ }
28+ }
29+
2030 async get ( url , headers = { } , query = { } , body = { } ) {
2131 url = url . startsWith ( '/' ) ? url : `/${ url } ` ;
2232 if ( Object . keys ( body ) . length !== 0 ) {
23- return this . request . get ( this . baseUrl + url )
33+ const res = await this . request . get ( this . baseUrl + url )
2434 . send ( body )
2535 . set ( headers )
2636 . query ( query ) ;
37+ this . setAllureAttachment ( res ) ;
38+ return res ;
2739 } else {
28- return this . request . get ( this . baseUrl + url )
40+ const res = await this . request . get ( this . baseUrl + url )
2941 . send ( { } )
3042 . set ( headers )
3143 . query ( query ) ;
44+ this . setAllureAttachment ( res ) ;
45+ return res ;
3246 }
3347 }
3448
3549 async post ( url , body = { } , headers = { } ) {
3650 url = url . startsWith ( '/' ) ? url : `/${ url } ` ;
37- return this . request . post ( this . baseUrl + url )
51+ const res = await this . request . post ( this . baseUrl + url )
3852 . send ( body )
3953 . set ( headers ) ;
54+ this . setAllureAttachment ( res ) ;
55+ return res ;
4056 }
4157
4258 async put ( url , body = { } , headers = { } ) {
4359 url = url . startsWith ( '/' ) ? url : `/${ url } ` ;
44- return this . request . put ( this . baseUrl + url )
60+ const res = await this . request . put ( this . baseUrl + url )
4561 . send ( body )
4662 . set ( headers ) ;
63+ this . setAllureAttachment ( res ) ;
64+ return res ;
4765 }
4866
4967 async patch ( url , body = { } , headers = { } ) {
5068 url = url . startsWith ( '/' ) ? url : `/${ url } ` ;
51- return this . request . patch ( this . baseUrl + url )
69+ const res = await this . request . patch ( this . baseUrl + url )
5270 . send ( body )
5371 . set ( headers ) ;
72+ this . setAllureAttachment ( res ) ;
73+ return res ;
5474 }
5575
5676 async delete ( url , headers = { } ) {
5777 url = url . startsWith ( '/' ) ? url : `/${ url } ` ;
58- return this . request . delete ( this . baseUrl + url )
78+ const res = await this . request . delete ( this . baseUrl + url )
5979 . set ( headers ) ;
80+ this . setAllureAttachment ( res ) ;
81+ return res ;
6082 }
6183}
6284
0 commit comments