Skip to content

Commit

Permalink
fix: transmit http body as bytes instead of string (#3254)
Browse files Browse the repository at this point in the history
* fix: transmit http body as bytes instead of string

* fix: update grpc body to []byte
  • Loading branch information
mathnogueira committed Oct 13, 2023
1 parent 0aeb269 commit e4debd9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
2 changes: 1 addition & 1 deletion agent/client/workflow_send_trigger_response_test.go
Expand Up @@ -29,7 +29,7 @@ func TestSendTriggerResult(t *testing.T) {
StatusCode: 200,
Status: "OK",
Headers: []*proto.HttpHeader{},
Body: `{"ok": true}`,
Body: []byte(`{"ok": true}`),
},
},
}
Expand Down
22 changes: 11 additions & 11 deletions agent/proto/orchestrator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions agent/proto/orchestrator.proto
Expand Up @@ -163,13 +163,13 @@ message HttpResponse {
int32 statusCode = 1;
string status = 2;
repeated HttpHeader headers = 3;
string body = 4;
bytes body = 4;
}

message GrpcResponse {
int32 statusCode = 1;
repeated GrpcHeader metadata = 2;
string body = 3;
bytes body = 3;
}

message TraceIdResponse {
Expand Down
2 changes: 1 addition & 1 deletion agent/proto/orchestrator_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions agent/workers/trigger.go
Expand Up @@ -254,7 +254,7 @@ func convertHttpResponseToProto(http *trigger.HTTPResponse) *proto.HttpResponse
StatusCode: int32(http.StatusCode),
Status: http.Status,
Headers: headers,
Body: http.Body,
Body: []byte(http.Body),
}
}

Expand All @@ -271,7 +271,7 @@ func convertGrpcResponseToProto(grpc *trigger.GRPCResponse) *proto.GrpcResponse
return &proto.GrpcResponse{
StatusCode: int32(grpc.StatusCode),
Metadata: headers,
Body: grpc.Body,
Body: []byte(grpc.Body),
}
}

Expand Down
48 changes: 47 additions & 1 deletion agent/workers/trigger_test.go
Expand Up @@ -61,12 +61,58 @@ func TestTrigger(t *testing.T) {
require.NotNil(t, response)
assert.Equal(t, "http", response.TriggerResult.Type)
assert.Equal(t, int32(http.StatusOK), response.TriggerResult.Http.StatusCode)
assert.JSONEq(t, `{"hello": "world"}`, response.TriggerResult.Http.Body)
assert.JSONEq(t, `{"hello": "world"}`, string(response.TriggerResult.Http.Body))

_, traceIdIsWatched := cache.Get(traceID)
assert.True(t, traceIdIsWatched)
}

func TestTriggerAgainstGoogle(t *testing.T) {
cache := collector.NewTraceCache()
controlPlane := mocks.NewGrpcServer()

client, err := client.Connect(context.Background(), controlPlane.Addr())
require.NoError(t, err)

triggerWorker := workers.NewTriggerWorker(client, workers.WithTraceCache(cache))

client.OnTriggerRequest(func(ctx context.Context, tr *proto.TriggerRequest) error {
err := triggerWorker.Trigger(ctx, tr)
assert.NoError(t, err, "trigger failed")
return err
})

client.Start(context.Background())

traceID := "42a2c381da1a5b3a32bc4988bf2431b0"

triggerRequest := &proto.TriggerRequest{
TestID: "my test",
RunID: 1,
TraceID: traceID,
Trigger: &proto.Trigger{
Type: "http",
Http: &proto.HttpRequest{
Method: "GET",
Url: "https://google.com",
Headers: []*proto.HttpHeader{
{Key: "Content-Type", Value: "application/json"},
},
},
},
}

// make the control plane send a trigger request to the agent
controlPlane.SendTriggerRequest(triggerRequest)
time.Sleep(1 * time.Second)

response := controlPlane.GetLastTriggerResponse()

require.NotNil(t, response)
assert.Equal(t, "http", response.TriggerResult.Type)
assert.Equal(t, int32(http.StatusOK), response.TriggerResult.Http.StatusCode)
}

func createHelloWorldApi() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"hello": "world"}`))
Expand Down

0 comments on commit e4debd9

Please sign in to comment.