Skip to content

Commit

Permalink
add test and fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Jan 17, 2025
1 parent 59a91bf commit 4b1897a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cloud/gcp/runtime/gateway/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func (g *gcpMiddleware) handleSubscription(opts *gateway.GatewayStartOpts) fasth
var message topicpb.TopicMessage
err := proto.Unmarshal(pubsubEvent.Message.Data, &message)
if err != nil {
fmt.Print("could not parse message as a nitric event attempting to parse as generic json payload")
fmt.Println("could not parse message as a nitric event attempting to parse as generic json payload")

messageData := map[string]interface{}{}
err := json.Unmarshal(pubsubEvent.Message.Data, &message)
messageData := map[string]any{}
err := json.Unmarshal(pubsubEvent.Message.Data, &messageData)
if err != nil {
ctx.Error("could not unmarshal event data", 500)
return
Expand Down
59 changes: 59 additions & 0 deletions cloud/gcp/runtime/gateway/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,64 @@ var _ = Describe("Http", func() {
Expect(string(responseBody)).To(Equal("success"))
})
})

When("From a subscription with a JSON event", func() {
content := map[string]interface{}{
"Test": "Test",
}

messageBytes, err := json.Marshal(content)
Expect(err).To(BeNil())

b64Event := base64.StdEncoding.EncodeToString(messageBytes)
payloadBytes, _ := json.Marshal(&map[string]interface{}{
"subscription": "test",
"message": map[string]interface{}{
"attributes": map[string]string{
"x-nitric-topic": "test",
},
"id": "test",
"data": b64Event,
},
})

It("Should handle the event successfully", func() {
var capturedRequest *topicspb.ServerMessage

By("Handling exactly 1 request")
mockTopicRequestHandler.EXPECT().HandleRequest(gomock.Any()).Times(1).DoAndReturn(func(arg0 interface{}) (*topicspb.ClientMessage, error) {
capturedRequest = arg0.(*topicspb.ServerMessage)

return &topicspb.ClientMessage{
Id: "test",
Content: &topicspb.ClientMessage_MessageResponse{
MessageResponse: &topicspb.MessageResponse{
Success: true,
},
},
}, nil
})

request, err := http.NewRequest("POST", fmt.Sprintf("%s/x-nitric-topic/test", gatewayUrl), bytes.NewReader(payloadBytes))
Expect(err).To(BeNil())
request.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(request)
Expect(err).To(BeNil())
responseBody, _ := io.ReadAll(resp.Body)

By("Not returning an error")
Expect(err).To(BeNil())

capturedMessageBody := capturedRequest.GetMessageRequest().GetMessage().GetStructPayload().AsMap()
By("Having the orignal payload translated")
Expect(capturedMessageBody["Test"]).To(Equal("Test"))

By("The request returns a successful status")
Expect(resp.StatusCode).To(Equal(200))

By("Returning the expected output")
Expect(string(responseBody)).To(Equal("success"))
})
})
})
})

0 comments on commit 4b1897a

Please sign in to comment.