Skip to content

Commit

Permalink
req/rep after_response: use cxHead for headers instead of plain Dict …
Browse files Browse the repository at this point in the history
…to uniformize APIs

Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Nov 9, 2024
1 parent 7c002d2 commit e97a1d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 71 deletions.
28 changes: 11 additions & 17 deletions pkg/runtime/ctx_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ def ctx_request_headers_frozen(ctx):
"""
A request's headers are immutable.
If they were mutable, the following line would pass:
assert that(ctx.request.headers).does_not_contain_key("set")
Args:
ctx: the context that Monkey provides.
"""
assert that(ctx.request.headers).has_size(1)
assert that(dict(ctx.request.headers)).has_size(1)
JSON_MIME = "application/json"
assert that(ctx.request.headers["Accept"]).is_equal_to([JSON_MIME])
ctx.request.headers["set"] = ["some", "values"]
assert that(ctx.request.headers.get("Accept")).is_equal_to(JSON_MIME)
ctx.request.headers.set("set", ["some", "values"])
monkey.check(
name = "ctx_request_headers_frozen",
Expand All @@ -71,8 +68,8 @@ monkey.check(
require.Equal(t, []string{
"*starlark.EvalError",
"Traceback (most recent call last):",
" fuzzymonkey.star:14:24: in ctx_request_headers_frozen",
"Error: cannot insert into frozen hash table",
" fuzzymonkey.star:11:24: in ctx_request_headers_frozen",
"Error: cannot set frozen hash table",
}, v.Reason)
require.NotEmpty(t, v.ElapsedNs)
require.NotEmpty(t, v.ExecutionSteps)
Expand Down Expand Up @@ -170,13 +167,10 @@ def ctx_response_headers_frozen(ctx):
"""
A response's headers are immutable.
If they were mutable, the following line would pass:
assert that(ctx.response.headers).does_not_contain_key("set")
Args:
ctx: the context that Monkey provides.
"""
assert that(ctx.response.headers).has_size(11)
assert that(dict(ctx.response.headers)).has_size(11)
HEADERS = [
"Access-Control-Allow-Credentials",
"Age",
Expand All @@ -190,9 +184,9 @@ def ctx_response_headers_frozen(ctx):
"Expect-Ct",
"Expires",
]
assert that(ctx.response.headers).contains_all_in(HEADERS).in_order()
assert that(ctx.response.headers["Age"]).is_equal_to(["0"])
ctx.response.headers["set"] = ["some", "values"]
assert that(dict(ctx.response.headers)).contains_all_in(HEADERS).in_order()
assert that(ctx.response.headers.get("Age")).is_equal_to("0")
ctx.response.headers.set("set", ["some", "values"])
monkey.check(
name = "ctx_response_headers_frozen",
Expand All @@ -207,8 +201,8 @@ monkey.check(
require.Equal(t, []string{
"*starlark.EvalError",
"Traceback (most recent call last):",
" fuzzymonkey.star:27:25: in ctx_response_headers_frozen",
"Error: cannot insert into frozen hash table",
" fuzzymonkey.star:24:25: in ctx_response_headers_frozen",
"Error: cannot set frozen hash table",
}, v.Reason)
require.NotEmpty(t, v.ElapsedNs)
require.NotEmpty(t, v.ExecutionSteps)
Expand Down
36 changes: 9 additions & 27 deletions pkg/runtime/cx_request_after_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@ type cxRequestAfterResponse struct {

protoBodyDecoded *structpb.Value
body starlark.Value

protoHeaders []*fm.HeaderPair
headers starlark.Value //FIXME: cxHeaders + Freeze
}

func newCxRequestAfterResponse(i *fm.Clt_CallRequestRaw_Input) (cr *cxRequestAfterResponse) {
cr = &cxRequestAfterResponse{
attrs: make(starlark.StringDict, 3),
}
switch x := i.GetInput().(type) {

case *fm.Clt_CallRequestRaw_Input_HttpRequest_:
cr.ty = cxRequestHttp
cr = &cxRequestAfterResponse{
ty: cxRequestHttp,
attrs: make(starlark.StringDict, 4),
}

reqProto := i.GetHttpRequest()
cr.attrs["method"] = starlark.String(reqProto.Method)
cr.attrs["url"] = starlark.String(reqProto.Url)
cr.attrs["content"] = starlark.String(reqProto.Body)
cr.protoHeaders = reqProto.Headers
headers := newcxHead(reqProto.Headers)
headers.Freeze()
cr.attrs["headers"] = headers
if reqProto.Body != nil {
cr.protoBodyDecoded = reqProto.BodyDecoded
}
Expand All @@ -63,14 +62,11 @@ func (m *cxRequestAfterResponse) Freeze() {
if m.body != nil {
m.body.Freeze()
}
if m.headers != nil {
m.headers.Freeze()
}
}

func (m *cxRequestAfterResponse) AttrNames() []string {
if m.attrnames == nil {
names := append(m.attrs.Keys(), "headers")
names := m.attrs.Keys()
if m.protoBodyDecoded != nil {
names = append(names, "body")
}
Expand All @@ -88,21 +84,7 @@ func (m *cxRequestAfterResponse) Attr(name string) (starlark.Value, error) {
m.body.Freeze()
}
return m.body, nil

case name == "headers":
if m.headers == nil {
var err error
if m.headers, err = headerPairs(m.protoHeaders); err != nil {
return nil, err
}
m.headers.Freeze()
}
return m.headers, nil

default:
if v := m.attrs[name]; v != nil {
return v, nil
}
return nil, nil // no such method
return m.attrs[name], nil
}
}
36 changes: 9 additions & 27 deletions pkg/runtime/cx_response_after_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ type cxResponseAfterResponse struct {

protoBodyDecoded *structpb.Value
body starlark.Value

protoHeaders []*fm.HeaderPair
headers starlark.Value //FIXME: cxHeaders + Freeze
}

func newCxResponseAfterResponse(o *fm.Clt_CallResponseRaw_Output) (cr *cxResponseAfterResponse) {
cr = &cxResponseAfterResponse{
attrs: make(starlark.StringDict, 5),
}
switch x := o.GetOutput().(type) {

case *fm.Clt_CallResponseRaw_Output_HttpResponse_:
cr.ty = cxResponseHttp
cr = &cxResponseAfterResponse{
ty: cxResponseHttp,
attrs: make(starlark.StringDict, 6),
}

repProto := o.GetHttpResponse()
cr.attrs["status_code"] = starlark.MakeUint(uint(repProto.StatusCode))
Expand All @@ -46,7 +43,9 @@ func newCxResponseAfterResponse(o *fm.Clt_CallResponseRaw_Output) (cr *cxRespons
cr.attrs["elapsed_ms"] = starlark.MakeInt64(repProto.ElapsedNs / 1.e6)
// "error": repProto.Error Checks make this unreachable
// "history" :: []Rep (redirects)?
cr.protoHeaders = repProto.Headers
headers := newcxHead(repProto.Headers)
headers.Freeze()
cr.attrs["headers"] = headers
if repProto.Body != nil {
cr.protoBodyDecoded = repProto.BodyDecoded
}
Expand All @@ -71,14 +70,11 @@ func (m *cxResponseAfterResponse) Freeze() {
if m.body != nil {
m.body.Freeze()
}
if m.headers != nil {
m.headers.Freeze()
}
}

func (m *cxResponseAfterResponse) AttrNames() []string {
if m.attrnames == nil {
names := append(m.attrs.Keys(), "headers")
names := m.attrs.Keys()
if m.protoBodyDecoded != nil {
names = append(names, "body")
}
Expand All @@ -96,21 +92,7 @@ func (m *cxResponseAfterResponse) Attr(name string) (starlark.Value, error) {
m.body.Freeze()
}
return m.body, nil

case name == "headers":
if m.headers == nil {
var err error
if m.headers, err = headerPairs(m.protoHeaders); err != nil {
return nil, err
}
m.headers.Freeze()
}
return m.headers, nil

default:
if v := m.attrs[name]; v != nil {
return v, nil
}
return nil, nil // no such method
return m.attrs[name], nil
}
}

0 comments on commit e97a1d1

Please sign in to comment.