@@ -571,7 +571,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
571
571
} ) ;
572
572
573
573
it ( "422 error with details" , async ( ) => {
574
- expect . assertions ( 8 ) ;
574
+ expect . assertions ( 9 ) ;
575
575
576
576
const request = await mockRequestHttpServer ( ( req , res ) => {
577
577
expect ( req . method ) . toBe ( "POST" ) ;
@@ -607,6 +607,9 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
607
607
throw new Error ( "should not resolve" ) ;
608
608
} catch ( error ) {
609
609
expect ( error . status ) . toEqual ( 422 ) ;
610
+ expect ( error . message ) . toEqual (
611
+ 'Validation Failed: {"resource":"Label","code":"invalid","field":"color"} - https://developer.github.com/v3/issues/labels/#create-a-label' ,
612
+ ) ;
610
613
expect ( error . response . headers [ "x-foo" ] ) . toEqual ( "bar" ) ;
611
614
expect ( error . response . data . documentation_url ) . toEqual (
612
615
"https://developer.github.com/v3/issues/labels/#create-a-label" ,
@@ -619,6 +622,114 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
619
622
}
620
623
} ) ;
621
624
625
+ it ( "422 error with details without documentation_url" , async ( ) => {
626
+ expect . assertions ( 9 ) ;
627
+
628
+ const request = await mockRequestHttpServer ( ( req , res ) => {
629
+ expect ( req . method ) . toBe ( "POST" ) ;
630
+ expect ( req . url ) . toBe ( "/repos/octocat/hello-world/labels" ) ;
631
+ expect ( req . headers . accept ) . toBe ( "application/vnd.github.v3+json" ) ;
632
+ expect ( req . headers [ "user-agent" ] ) . toBe ( userAgent ) ;
633
+
634
+ res . writeHead ( 422 , {
635
+ "Content-Type" : "application/json; charset=utf-8" ,
636
+ "X-Foo" : "bar" ,
637
+ } ) ;
638
+ res . end (
639
+ JSON . stringify ( {
640
+ message : "Validation Failed" ,
641
+ errors : [
642
+ {
643
+ resource : "Label" ,
644
+ code : "invalid" ,
645
+ field : "color" ,
646
+ } ,
647
+ ] ,
648
+ } ) ,
649
+ ) ;
650
+ } ) ;
651
+
652
+ try {
653
+ await request ( "POST /repos/octocat/hello-world/labels" , {
654
+ name : "foo" ,
655
+ color : "invalid" ,
656
+ } ) ;
657
+ throw new Error ( "should not resolve" ) ;
658
+ } catch ( error ) {
659
+ expect ( error . status ) . toEqual ( 422 ) ;
660
+ expect ( error . message ) . toEqual (
661
+ 'Validation Failed: {"resource":"Label","code":"invalid","field":"color"}' ,
662
+ ) ;
663
+ expect ( error . response . headers [ "x-foo" ] ) . toEqual ( "bar" ) ;
664
+ expect ( error . response . data . documentation_url ) . toEqual ( undefined ) ;
665
+ expect ( error . response . data . errors ) . toEqual ( [
666
+ { resource : "Label" , code : "invalid" , field : "color" } ,
667
+ ] ) ;
668
+ } finally {
669
+ request . closeMockServer ( ) ;
670
+ }
671
+ } ) ;
672
+
673
+ it ( "422 error with ArrayBuffer as body" , async ( ) => {
674
+ expect . assertions ( 7 ) ;
675
+
676
+ const request = await mockRequestHttpServer ( ( req , res ) => {
677
+ expect ( req . method ) . toBe ( "POST" ) ;
678
+ expect ( req . url ) . toBe ( "/repos/octocat/hello-world/labels" ) ;
679
+ expect ( req . headers . accept ) . toBe ( "application/vnd.github.v3+json" ) ;
680
+ expect ( req . headers [ "user-agent" ] ) . toBe ( userAgent ) ;
681
+
682
+ res . writeHead ( 422 , {
683
+ "Content-Type" : "application/octet-stream" ,
684
+ } ) ;
685
+ res . end ( "payload" ) ;
686
+ } ) ;
687
+
688
+ try {
689
+ await request ( "POST /repos/octocat/hello-world/labels" , {
690
+ name : "foo" ,
691
+ color : "invalid" ,
692
+ } ) ;
693
+ throw new Error ( "should not resolve" ) ;
694
+ } catch ( error ) {
695
+ expect ( error . status ) . toEqual ( 422 ) ;
696
+ expect ( error . message ) . toEqual ( "Unknown error" ) ;
697
+ expect ( error . response . data ) . instanceOf ( ArrayBuffer ) ;
698
+ } finally {
699
+ request . closeMockServer ( ) ;
700
+ }
701
+ } ) ;
702
+
703
+ it ( "422 error without body" , async ( ) => {
704
+ expect . assertions ( 7 ) ;
705
+
706
+ const request = await mockRequestHttpServer ( ( req , res ) => {
707
+ expect ( req . method ) . toBe ( "POST" ) ;
708
+ expect ( req . url ) . toBe ( "/repos/octocat/hello-world/labels" ) ;
709
+ expect ( req . headers . accept ) . toBe ( "application/vnd.github.v3+json" ) ;
710
+ expect ( req . headers [ "user-agent" ] ) . toBe ( userAgent ) ;
711
+
712
+ res . writeHead ( 422 , {
713
+ "Content-Type" : "text/plain" ,
714
+ } ) ;
715
+ res . end ( ) ;
716
+ } ) ;
717
+
718
+ try {
719
+ await request ( "POST /repos/octocat/hello-world/labels" , {
720
+ name : "foo" ,
721
+ color : "invalid" ,
722
+ } ) ;
723
+ throw new Error ( "should not resolve" ) ;
724
+ } catch ( error ) {
725
+ expect ( error . status ) . toEqual ( 422 ) ;
726
+ expect ( error . message ) . toEqual ( "" ) ;
727
+ expect ( error . response . data ) . toEqual ( "" ) ;
728
+ } finally {
729
+ request . closeMockServer ( ) ;
730
+ }
731
+ } ) ;
732
+
622
733
it ( "redacts credentials from error.request.headers.authorization" , async ( ) => {
623
734
expect . assertions ( 5 ) ;
624
735
@@ -967,6 +1078,35 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
967
1078
expect ( error . response . data . documentation_url ) . toEqual (
968
1079
"https://docs.github.com/en/rest/reference/repos#get-a-repository" ,
969
1080
) ;
1081
+ } finally {
1082
+ request . closeMockServer ( ) ;
1083
+ }
1084
+ } ) ;
1085
+
1086
+ it ( "404 not found without documentation_url" , { skip : true } , async ( ) => {
1087
+ expect . assertions ( 3 ) ;
1088
+
1089
+ const request = await mockRequestHttpServer ( async ( req , res ) => {
1090
+ expect ( req . method ) . toBe ( "GET" ) ;
1091
+ expect ( req . url ) . toBe ( "/repos/octocat/unknown" ) ;
1092
+
1093
+ res . writeHead ( 404 ) ;
1094
+ res . end (
1095
+ JSON . stringify ( {
1096
+ message : "Not Found" ,
1097
+ } ) ,
1098
+ ) ;
1099
+ } ) ;
1100
+
1101
+ try {
1102
+ await request ( "GET /repos/octocat/unknown" ) ;
1103
+ throw new Error ( "Should have thrown" ) ;
1104
+ } catch ( error ) {
1105
+ expect ( error . status ) . toEqual ( 404 ) ;
1106
+ expect ( error . response . data . message ) . toEqual ( "Not Found" ) ;
1107
+ expect ( error . response . data . documentation_url ) . toEqual ( "" ) ;
1108
+ } finally {
1109
+ request . closeMockServer ( ) ;
970
1110
}
971
1111
} ) ;
972
1112
0 commit comments