@@ -643,6 +643,97 @@ class RequestTest {
643
643
}
644
644
}
645
645
646
+ @Test
647
+ fun curlGet () {
648
+ val request =
649
+ Request
650
+ .Builder ()
651
+ .url(" https://example.com" )
652
+ .header(" Authorization" , " Bearer abc123" )
653
+ .build()
654
+ val curl = request.curl()
655
+ assertThat(curl).isEqualTo(" curl -H \" Authorization: Bearer abc123\" \" https://example.com/\" " )
656
+ }
657
+
658
+ @Test
659
+ fun curlPostWithBody () {
660
+ val mediaType = " application/json" .toMediaType()
661
+ val body = " {\" key\" :\" value\" }" .toRequestBody(mediaType)
662
+ val request =
663
+ Request
664
+ .Builder ()
665
+ .url(" https://api.example.com/data" )
666
+ .post(body)
667
+ .addHeader(" Content-Type" , " application/json" )
668
+ .addHeader(" Authorization" , " Bearer abc123" )
669
+ .build()
670
+ val curl = request.curl()
671
+ assertThat(
672
+ curl,
673
+ ).isEqualTo(
674
+ " curl -X POST -H \" Content-Type: application/json\" -H \" Authorization: Bearer abc123\" --data \" {\\\" key\\\" :\\\" value\\\" }\" \" https://api.example.com/data\" " ,
675
+ )
676
+ }
677
+
678
+ @Test
679
+ fun curlPostWithComplexBody () {
680
+ val mediaType = " application/json" .toMediaType()
681
+ val jsonBody =
682
+ """
683
+ {
684
+ "user": {
685
+ "id": 123,
686
+ "name": "John Doe"
687
+ },
688
+ "roles": ["admin", "editor"],
689
+ "active": true
690
+ }
691
+ """ .trimIndent()
692
+ val body = jsonBody.toRequestBody(mediaType)
693
+ val request =
694
+ Request
695
+ .Builder ()
696
+ .url(" https://api.example.com/users" )
697
+ .post(body)
698
+ .addHeader(" Content-Type" , " application/json" )
699
+ .addHeader(" Authorization" , " Bearer xyz789" )
700
+ .build()
701
+ val curl = request.curl()
702
+ assertThat(curl).isEqualTo(
703
+ " curl -X POST -H \" Content-Type: application/json\" -H \" Authorization: Bearer xyz789\" --data \" {\n " +
704
+ " \\\" user\\\" : {\n " +
705
+ " \\\" id\\\" : 123,\n " +
706
+ " \\\" name\\\" : \\\" John Doe\\\"\n " +
707
+ " },\n " +
708
+ " \\\" roles\\\" : [\\\" admin\\\" , \\\" editor\\\" ],\n " +
709
+ " \\\" active\\\" : true\n " +
710
+ " }\" \" https://api.example.com/users\" " ,
711
+ )
712
+ }
713
+
714
+ @Test
715
+ fun curlPostWithBinaryBody () {
716
+ val mediaType = " application/octet-stream" .toMediaType()
717
+ val binaryData = byteArrayOf(0x00 , 0x01 , 0x02 , 0x03 , 0x04 )
718
+
719
+ val body = RequestBody .create(mediaType, binaryData)
720
+
721
+ val request =
722
+ Request
723
+ .Builder ()
724
+ .url(" https://api.example.com/upload" )
725
+ .post(body)
726
+ .addHeader(" Content-Type" , " application/octet-stream" )
727
+ .build()
728
+
729
+ val curl = request.curl()
730
+ assertThat(
731
+ curl,
732
+ ).isEqualTo(
733
+ " curl -X POST -H \" Content-Type: application/octet-stream\" --data-binary \" [binary body omitted]\" \" https://api.example.com/upload\" " ,
734
+ )
735
+ }
736
+
646
737
private fun bodyToHex (body : RequestBody ): String {
647
738
val buffer = Buffer ()
648
739
body.writeTo(buffer)
0 commit comments