Skip to content

Commit

Permalink
fix(server): fix string wrapping for triggers (#4034)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbdias authored Oct 1, 2024
1 parent fe93126 commit 64237ce
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions server/executor/trigger/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kubeshop/tracetest/agent/workers/trigger"
triggerer "github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/test"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -295,3 +296,30 @@ func TestTriggerPostWithBearerAuth(t *testing.T) {
assert.Equal(t, 200, resp.Result.HTTP.StatusCode)
assert.Equal(t, "OK", resp.Result.HTTP.Body)
}

func TestTriggerResolveLargePayloadWithSingleQuote(t *testing.T) {
testObject := test.Test{
Name: "Run API to trigger Gemini ",
Trigger: trigger.Trigger{
Type: trigger.TriggerTypeHTTP,
HTTP: &trigger.HTTPRequest{
URL: "http://llm-api:8800/summarizeText",
Method: trigger.HTTPMethodPOST,
Headers: []trigger.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: "{\n \"provider\": \"Google (Gemini)\",\n \"text\": \"Born in London, Turing was raised in southern England. He graduated from King's College, Cambridge, and in 1938, earned a doctorate degree from Princeton University. During World War II, Turing worked for the Government Code and Cypher School at Bletchley Park, Britain's codebreaking centre that produced Ultra intelligence. He led Hut 8, the section responsible for German naval cryptanalysis. Turing devised techniques for speeding the breaking of German ciphers, including improvements to the pre-war Polish bomba method, an electromechanical machine that could find settings for the Enigma machine. He played a crucial role in cracking intercepted messages that enabled the Allies to defeat the Axis powers in many crucial engagements, including the Battle of the Atlantic.\\n\\nAfter the war, Turing worked at the National Physical Laboratory, where he designed the Automatic Computing Engine, one of the first designs for a stored-program computer. In 1948, Turing joined Max Newman's Computing Machine Laboratory at the Victoria University of Manchester, where he helped develop the Manchester computers[12] and became interested in mathematical biology. Turing wrote on the chemical basis of morphogenesis and predicted oscillating chemical reactions such as the Belousov–Zhabotinsky reaction, first observed in the 1960s. Despite these accomplishments, he was never fully recognised during his lifetime because much of his work was covered by the Official Secrets Act.\"\n }",
},
},
}

triggerOptions := &triggerer.ResolveOptions{
Executor: expression.NewExecutor(),
}

httpTriggerer := triggerer.HTTP()
resolvedTest, err := httpTriggerer.Resolve(context.Background(), testObject, triggerOptions)

assert.NoError(t, err)
assert.NotNil(t, resolvedTest)
}
2 changes: 2 additions & 0 deletions server/executor/trigger/triggerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"sync"

"github.com/kubeshop/tracetest/agent/workers/trigger"
Expand Down Expand Up @@ -74,5 +75,6 @@ func (r *Registry) Get(triggererType trigger.TriggerType) (Triggerer, error) {
}

func WrapInQuotes(input string, quoteChar string) string {
input = strings.ReplaceAll(input, quoteChar, fmt.Sprintf("\\%s", quoteChar))
return fmt.Sprintf("%s%s%s", quoteChar, input, quoteChar)
}

0 comments on commit 64237ce

Please sign in to comment.