Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
7cd38248
Commit
7cd38248
authored
Mar 21, 2026
by
Ethan0x0000
Browse files
test(ops): add tests for setOpsEndpointContext and safeUpstreamURL
parent
db9021f9
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/internal/handler/ops_error_logger_test.go
View file @
7cd38248
...
...
@@ -274,3 +274,42 @@ func TestNormalizeOpsErrorType(t *testing.T) {
})
}
}
func
TestSetOpsEndpointContext_SetsContextKeys
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/v1/messages"
,
nil
)
setOpsEndpointContext
(
c
,
"claude-3-5-sonnet-20241022"
,
int16
(
2
))
// stream
v
,
ok
:=
c
.
Get
(
opsUpstreamModelKey
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
"claude-3-5-sonnet-20241022"
,
v
.
(
string
))
rt
,
ok
:=
c
.
Get
(
opsRequestTypeKey
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
int16
(
2
),
rt
.
(
int16
))
}
func
TestSetOpsEndpointContext_EmptyModelNotStored
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
c
,
_
:=
gin
.
CreateTestContext
(
rec
)
c
.
Request
=
httptest
.
NewRequest
(
http
.
MethodPost
,
"/v1/messages"
,
nil
)
setOpsEndpointContext
(
c
,
""
,
int16
(
1
))
_
,
ok
:=
c
.
Get
(
opsUpstreamModelKey
)
require
.
False
(
t
,
ok
,
"empty upstream model should not be stored"
)
rt
,
ok
:=
c
.
Get
(
opsRequestTypeKey
)
require
.
True
(
t
,
ok
)
require
.
Equal
(
t
,
int16
(
1
),
rt
.
(
int16
))
}
func
TestSetOpsEndpointContext_NilContext
(
t
*
testing
.
T
)
{
require
.
NotPanics
(
t
,
func
()
{
setOpsEndpointContext
(
nil
,
"model"
,
int16
(
1
))
})
}
backend/internal/service/ops_upstream_context_test.go
View file @
7cd38248
...
...
@@ -8,6 +8,27 @@ import (
"github.com/stretchr/testify/require"
)
func
TestSafeUpstreamURL
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
input
string
want
string
}{
{
"strips query"
,
"https://api.anthropic.com/v1/messages?beta=true"
,
"https://api.anthropic.com/v1/messages"
},
{
"strips fragment"
,
"https://api.openai.com/v1/responses#frag"
,
"https://api.openai.com/v1/responses"
},
{
"strips both"
,
"https://host/path?token=secret#x"
,
"https://host/path"
},
{
"no query or fragment"
,
"https://host/path"
,
"https://host/path"
},
{
"empty string"
,
""
,
""
},
{
"whitespace only"
,
" "
,
""
},
{
"query before fragment"
,
"https://h/p?a=1#f"
,
"https://h/p"
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
tt
.
want
,
safeUpstreamURL
(
tt
.
input
))
})
}
}
func
TestAppendOpsUpstreamError_UsesRequestBodyBytesFromContext
(
t
*
testing
.
T
)
{
gin
.
SetMode
(
gin
.
TestMode
)
rec
:=
httptest
.
NewRecorder
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment