Skip to content

Commit

Permalink
check unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumeet Rai committed Nov 14, 2024
1 parent caefacf commit 5878e3f
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions plugins/internal/tengoutil/secure_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,62 @@ func TestNewSecureScript(t *testing.T) {
_, err = s.Compile()
assert.NoError(t, err)
})

t.Run("HTTP GET with headers", func(t *testing.T) {
s, err := NewSecureScript([]byte(heredoc.Doc(`
http := import("http")
headers := { "User-Agent": "test-agent", "Accept": "application/json" }
response := http.get("http://example.com", headers)
response.body
`)), nil)
assert.NoError(t, err)
_, err = s.Compile()
assert.NoError(t, err)
})

t.Run("HTTP GET with invalid URL argument type", func(t *testing.T) {
s, err := NewSecureScript([]byte(heredoc.Doc(`
http := import("http")
http.get(12345)
`)), nil)
assert.NoError(t, err)
compiledScript, err := s.Compile()
assert.NoError(t, err)

_, err = compiledScript.Run()

Check failure on line 91 in plugins/internal/tengoutil/secure_script_test.go

View workflow job for this annotation

GitHub Actions / plugins-test (internal/tengoutil)

assignment mismatch: 2 variables but compiledScript.Run returns 1 value
assert.Error(t, err)
assert.Contains(t, err.Error(), "expected argument 1 (URL) to be a string")
})

t.Run("HTTP GET with invalid header value type", func(t *testing.T) {
s, err := NewSecureScript([]byte(heredoc.Doc(`
http := import("http")
headers := { "User-Agent": 12345 }
http.get("http://example.com", headers)
`)), nil)
assert.NoError(t, err)
compiledScript, err := s.Compile()
assert.NoError(t, err)

_, err = compiledScript.Run()

Check failure on line 106 in plugins/internal/tengoutil/secure_script_test.go

View workflow job for this annotation

GitHub Actions / plugins-test (internal/tengoutil)

assignment mismatch: 2 variables but compiledScript.Run returns 1 value
assert.Error(t, err)
assert.Contains(t, err.Error(), "header value for key 'User-Agent' must be a string")
})

t.Run("HTTP GET with timeout", func(t *testing.T) {
s, err := NewSecureScript([]byte(heredoc.Doc(`
http := import("http")
response := http.get("http://example.com")
response.body
`)), nil)
assert.NoError(t, err)

defaultTimeout = 1 * time.Millisecond

Check failure on line 119 in plugins/internal/tengoutil/secure_script_test.go

View workflow job for this annotation

GitHub Actions / plugins-test (internal/tengoutil)

undefined: time
compiledScript, err := s.Compile()
assert.NoError(t, err)

_, err = compiledScript.Run()

Check failure on line 123 in plugins/internal/tengoutil/secure_script_test.go

View workflow job for this annotation

GitHub Actions / plugins-test (internal/tengoutil)

assignment mismatch: 2 variables but compiledScript.Run returns 1 value
assert.Error(t, err)
assert.Contains(t, err.Error(), "context deadline exceeded")
})
}

0 comments on commit 5878e3f

Please sign in to comment.