11import axios from "axios" ;
22import { describe , it } from "node:test" ;
3- import generateCurl , { bindCurl , curlInterceptor } from "../lib" ;
3+ import { bindCurl , curlInterceptor } from "../lib" ;
44import assert from "node:assert" ;
55
66describe ( 'curl-generator' , ( ) => {
77
88 it ( '[1] should generate curl from simple GET' , async ( ) => {
99
1010 var command = '' ;
11- const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '" ;
11+ const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json '" ;
1212
1313 const instance = axios . create ( ) ;
1414 const callback = ( cmd : string ) => command = cmd ;
@@ -23,7 +23,7 @@ describe('curl-generator', () => {
2323 it ( '[2] should generate curl from GET with params' , async ( ) => {
2424
2525 var command = '' ;
26- const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1?animal=berry' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: '" ;
26+ const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1?animal=berry' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json '" ;
2727
2828 const instance = axios . create ( ) ;
2929 const callback = ( cmd : string ) => command = cmd ;
@@ -40,7 +40,7 @@ describe('curl-generator', () => {
4040 it ( '[3] should generate curl from GET with auth' , async ( ) => {
4141
4242 var command = '' ;
43- const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --user 'berry:12345'" ;
43+ const expected = "curl 'https://pokeapi.co/api/v2/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json ' --user 'berry:12345'" ;
4444
4545 const instance = axios . create ( ) ;
4646 const callback = ( cmd : string ) => command = cmd ;
@@ -78,7 +78,7 @@ describe('curl-generator', () => {
7878 it ( '[5] should generate curl from GET using baseURL' , async ( ) => {
7979
8080 var command = '' ;
81- const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'" ;
81+ const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json ' --url 'https://pokeapi.co/api/v2'" ;
8282
8383 const instance = axios . create ( { baseURL : 'https://pokeapi.co/api/v2' } ) ;
8484 const callback = ( cmd : string ) => command = cmd ;
@@ -92,12 +92,33 @@ describe('curl-generator', () => {
9292
9393 it ( '[6] should generate curl from GET using baseURL' , async ( ) => {
9494
95- const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: ' --url 'https://pokeapi.co/api/v2'" ;
95+ const expected = "curl '/berry-flavor/1' -X GET -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json ' --url 'https://pokeapi.co/api/v2'" ;
9696
9797 const instance = bindCurl ( axios . create ( { baseURL : 'https://pokeapi.co/api/v2' } ) ) ;
9898
9999 await instance . get ( '/berry-flavor/1' ) ;
100100
101101 assert ( instance . curl === expected , 'O curl [6] gerado ficou diferente' ) ;
102102 } ) ;
103+
104+ it ( '[7] should generate curl from POST with auth and content type text' , async ( ) => {
105+
106+ var command = '' ;
107+ const expected = `curl 'https://jsonplaceholder.typicode.com/posts' -X POST -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/text' -H 'Authorization: Bearer token...' --data-raw '{"userName":"sample"}'` ;
108+ const instance = axios . create ( ) ;
109+ const callback = ( cmd : string ) => command = cmd ;
110+
111+ instance . interceptors . request . use ( ( req ) => curlInterceptor ( req , callback ) ) ;
112+
113+ await instance . post ( 'https://jsonplaceholder.typicode.com/posts' , { userName : 'sample' } ,
114+ {
115+ headers : {
116+ 'Content-Type' : 'application/text' ,
117+ 'Authorization' : 'Bearer token...'
118+ }
119+ }
120+ ) ;
121+
122+ assert ( command === expected , 'O curl [7] gerado ficou diferente' ) ;
123+ } ) ;
103124} ) ;
0 commit comments