1
1
import axios from "axios" ;
2
2
import { describe , it } from "node:test" ;
3
- import generateCurl , { bindCurl , curlInterceptor } from "../lib" ;
3
+ import { bindCurl , curlInterceptor } from "../lib" ;
4
4
import assert from "node:assert" ;
5
5
6
6
describe ( 'curl-generator' , ( ) => {
7
7
8
8
it ( '[1] should generate curl from simple GET' , async ( ) => {
9
9
10
10
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 '" ;
12
12
13
13
const instance = axios . create ( ) ;
14
14
const callback = ( cmd : string ) => command = cmd ;
@@ -23,7 +23,7 @@ describe('curl-generator', () => {
23
23
it ( '[2] should generate curl from GET with params' , async ( ) => {
24
24
25
25
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 '" ;
27
27
28
28
const instance = axios . create ( ) ;
29
29
const callback = ( cmd : string ) => command = cmd ;
@@ -40,7 +40,7 @@ describe('curl-generator', () => {
40
40
it ( '[3] should generate curl from GET with auth' , async ( ) => {
41
41
42
42
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'" ;
44
44
45
45
const instance = axios . create ( ) ;
46
46
const callback = ( cmd : string ) => command = cmd ;
@@ -78,7 +78,7 @@ describe('curl-generator', () => {
78
78
it ( '[5] should generate curl from GET using baseURL' , async ( ) => {
79
79
80
80
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'" ;
82
82
83
83
const instance = axios . create ( { baseURL : 'https://pokeapi.co/api/v2' } ) ;
84
84
const callback = ( cmd : string ) => command = cmd ;
@@ -92,12 +92,33 @@ describe('curl-generator', () => {
92
92
93
93
it ( '[6] should generate curl from GET using baseURL' , async ( ) => {
94
94
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'" ;
96
96
97
97
const instance = bindCurl ( axios . create ( { baseURL : 'https://pokeapi.co/api/v2' } ) ) ;
98
98
99
99
await instance . get ( '/berry-flavor/1' ) ;
100
100
101
101
assert ( instance . curl === expected , 'O curl [6] gerado ficou diferente' ) ;
102
102
} ) ;
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
+ } ) ;
103
124
} ) ;
0 commit comments