@@ -3,6 +3,8 @@ package app
3
3
import (
4
4
"net/http"
5
5
"testing"
6
+
7
+ "github.com/pact-foundation/pact-go/dsl"
6
8
)
7
9
8
10
func TestLargePactResponse (t * testing.T ) {
@@ -592,9 +594,9 @@ func TestArrayBodyRequestUnmatchedRequestBody(t *testing.T) {
592
594
a_request_is_sent_with ("application/json" , tc .unmatchedReqBody )
593
595
594
596
then .
595
- // Pact Mock Server returns 500 if request body does not match,
596
- // so the response status code is not checked
597
- pact_verification_is_not_successful ( )
597
+ // Pact Mock Server returns 500 if request body does not match
598
+ pact_verification_is_not_successful (). and ().
599
+ the_response_is_ ( http . StatusInternalServerError )
598
600
})
599
601
}
600
602
}
@@ -637,3 +639,183 @@ func TestArrayBodyRequestConstraintDoesNotMatch(t *testing.T) {
637
639
})
638
640
}
639
641
}
642
+
643
+ func TestArrayNestedWithinBody (t * testing.T ) {
644
+ pactReqBody := map [string ]interface {}{
645
+ "entries" : []interface {}{
646
+ map [string ]string {"key" : "val" },
647
+ map [string ]string {"key" : "val" },
648
+ },
649
+ }
650
+ const respContentType = "application/json"
651
+ const respBody = `[{"status":"ok"}]`
652
+
653
+ const matchedReqBody = `{"entries": [ {"key": "val"}, {"key": "val"} ]}`
654
+ const unmatchedReqBody = `{"entries": [ {"key": "val"}, {"key": "unexpected value"} ]}`
655
+
656
+ const matchedConstraintPath = "$.body.entries[0].key"
657
+ const matchedConstraintValue = "val"
658
+
659
+ const unmatchedConstraintPath = "$.body.entries[1].key"
660
+ const unmatchedConstraintValue = "wrong value"
661
+
662
+ const outOfBoundsConstraintPath = "$.body.entries[2].key"
663
+
664
+ t .Run ("Matches" , func (t * testing.T ) {
665
+ given , when , then := NewProxyStage (t )
666
+
667
+ given .
668
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
669
+
670
+ when .
671
+ a_request_is_sent_with ("application/json" , matchedReqBody )
672
+
673
+ then .
674
+ pact_verification_is_successful ().and ().
675
+ the_response_is_ (http .StatusOK ).and ().
676
+ the_response_body_is (respBody )
677
+ })
678
+
679
+ t .Run ("Does not match" , func (t * testing.T ) {
680
+ given , when , then := NewProxyStage (t )
681
+
682
+ given .
683
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
684
+
685
+ when .
686
+ a_request_is_sent_with ("application/json" , unmatchedReqBody )
687
+
688
+ then .
689
+ pact_verification_is_not_successful ().and ().
690
+ the_response_is_ (http .StatusBadRequest )
691
+ })
692
+
693
+ t .Run ("Matches with additional constraint" , func (t * testing.T ) {
694
+ given , when , then := NewProxyStage (t )
695
+
696
+ given .
697
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody ).and ().
698
+ an_additional_constraint_is_added (matchedConstraintPath , matchedConstraintValue )
699
+
700
+ when .
701
+ a_request_is_sent_with ("application/json" , matchedReqBody )
702
+
703
+ then .
704
+ pact_verification_is_successful ().and ().
705
+ the_response_is_ (http .StatusOK ).and ().
706
+ the_response_body_is (respBody )
707
+ })
708
+
709
+ t .Run ("Does not match due to additional constraint with different value" , func (t * testing.T ) {
710
+ given , when , then := NewProxyStage (t )
711
+
712
+ given .
713
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody ).and ().
714
+ an_additional_constraint_is_added (unmatchedConstraintPath , unmatchedConstraintValue )
715
+
716
+ when .
717
+ a_request_is_sent_with ("application/json" , matchedReqBody )
718
+
719
+ then .
720
+ pact_verification_is_not_successful ().and ().
721
+ the_response_is_ (http .StatusBadRequest )
722
+ })
723
+
724
+ t .Run ("Does not match due to additional constraint out of array bounds" , func (t * testing.T ) {
725
+ given , when , then := NewProxyStage (t )
726
+
727
+ given .
728
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody ).and ().
729
+ an_additional_constraint_is_added (outOfBoundsConstraintPath , matchedConstraintValue )
730
+
731
+ when .
732
+ a_request_is_sent_with ("application/json" , matchedReqBody )
733
+
734
+ then .
735
+ pact_verification_is_not_successful ().and ().
736
+ the_response_is_ (http .StatusBadRequest )
737
+ })
738
+ }
739
+
740
+ func TestArrayNestedWithinBodyContainingMatchers (t * testing.T ) {
741
+ pactReqBody := map [string ]interface {}{
742
+ "entries" : []interface {}{
743
+ map [string ]any {"key" : "val" },
744
+ map [string ]any {"key" : dsl .Term ("a" , "(a|b)" )},
745
+ },
746
+ }
747
+ const respContentType = "application/json"
748
+ const respBody = `[{"status":"ok"}]`
749
+
750
+ const matchedReqBody = `{"entries": [ {"key": "val"}, {"key": "a"} ]}`
751
+ const unmatchedReqBody = `{"entries": [ {"key": "val"}, {"key": "c"} ]}`
752
+
753
+ t .Run ("Matches" , func (t * testing.T ) {
754
+ given , when , then := NewProxyStage (t )
755
+
756
+ given .
757
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
758
+
759
+ when .
760
+ a_request_is_sent_with ("application/json" , matchedReqBody )
761
+
762
+ then .
763
+ pact_verification_is_successful ().and ().
764
+ the_response_is_ (http .StatusOK ).and ().
765
+ the_response_body_is (respBody )
766
+ })
767
+
768
+ t .Run ("Does not match" , func (t * testing.T ) {
769
+ given , when , then := NewProxyStage (t )
770
+
771
+ given .
772
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
773
+
774
+ when .
775
+ a_request_is_sent_with ("application/json" , unmatchedReqBody )
776
+
777
+ then .
778
+ pact_verification_is_not_successful ().and ().
779
+ the_response_is_ (http .StatusInternalServerError )
780
+ })
781
+ }
782
+
783
+ func TestEmptyArrayNestedWithinBody (t * testing.T ) {
784
+ pactReqBody := map [string ]interface {}{
785
+ "entries" : []interface {}{},
786
+ }
787
+ const respContentType = "application/json"
788
+ const respBody = `[{"status":"ok"}]`
789
+
790
+ const matchedReqBody = `{"entries": []}`
791
+ const unmatchedReqBody = `{"entries": [ {"key": "val"} ]}`
792
+
793
+ t .Run ("Matches" , func (t * testing.T ) {
794
+ given , when , then := NewProxyStage (t )
795
+
796
+ given .
797
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
798
+
799
+ when .
800
+ a_request_is_sent_with ("application/json" , matchedReqBody )
801
+
802
+ then .
803
+ pact_verification_is_successful ().and ().
804
+ the_response_is_ (http .StatusOK ).and ().
805
+ the_response_body_is (respBody )
806
+ })
807
+
808
+ t .Run ("Does not match" , func (t * testing.T ) {
809
+ given , when , then := NewProxyStage (t )
810
+
811
+ given .
812
+ a_pact_that_expects ("application/json" , pactReqBody , respContentType , respBody )
813
+
814
+ when .
815
+ a_request_is_sent_with ("application/json" , unmatchedReqBody )
816
+
817
+ then .
818
+ pact_verification_is_not_successful ().and ().
819
+ the_response_is_ (http .StatusInternalServerError )
820
+ })
821
+ }
0 commit comments