@@ -60,3 +60,45 @@ func TestExamplePublisher(t *testing.T) {
6060
6161 }
6262}
63+
64+ func TestExamplePublisherError (t * testing.T ) {
65+ t .Setenv ("PUBSUB_BROKER" , "localhost:1012" )
66+ t .Setenv ("HTTP_PORT" , "8200" )
67+
68+ const host = "http://localhost:8200"
69+ go main ()
70+ time .Sleep (time .Second * 1 )
71+
72+ testCases := []struct {
73+ desc string
74+ path string
75+ body []byte
76+ expectedStatusCode int
77+ expectedError error
78+ }{
79+ {
80+ desc : "valid order" ,
81+ path : "/publish-order" ,
82+ body : []byte (`{"data":{"orderId":"123","status":"pending"}}` ),
83+ expectedStatusCode : http .StatusInternalServerError ,
84+ },
85+ {
86+ desc : "valid product" ,
87+ path : "/publish-product" ,
88+ body : []byte (`{"data":{"productId":"123","price":"599"}}` ),
89+ expectedStatusCode : http .StatusInternalServerError ,
90+ },
91+ }
92+
93+ c := http.Client {}
94+
95+ for i , tc := range testCases {
96+ req , _ := http .NewRequest (http .MethodPost , host + tc .path , bytes .NewBuffer (tc .body ))
97+ resp , err := c .Do (req )
98+
99+ assert .Equal (t , tc .expectedStatusCode , resp .StatusCode , "TEST[%d], Failed.\n %s" , i , tc .desc )
100+ assert .NoError (t , err , "TEST[%d], Failed.\n %s" , i , tc .desc )
101+
102+ defer resp .Body .Close ()
103+ }
104+ }
0 commit comments