Skip to content

Commit

Permalink
header_authorization: finish replacing special handling of this speci…
Browse files Browse the repository at this point in the history
…fic header with before_request=..

Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed Nov 9, 2024
1 parent 89a4aad commit 4d35c19
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 522 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ monkey.openapi3(
# Note: references to schemas in `file` are resolved relative to file's location.
file = SPEC,
host = "https://jsonplaceholder.typicode.com",
# header_authorization = "Bearer {}".format(monkey.env("DEV_API_TOKEN")),
)

# Note: exec commands are executed in shells sharing the same environment variables,
Expand Down Expand Up @@ -153,9 +152,13 @@ def add_special_headers(ctx):

assert that(MY_HEADER.title()).is_equal_to(MY_HEADER)
assert that(dict(req.headers)).does_not_contain_key(MY_HEADER)
req.headers.set(MY_HEADER, "value!")
req.headers.add(MY_HEADER, "value!")
print("Added an extra header:", MY_HEADER)

# Let's also set a bearer token:
token = monkey.env("DEV_API_TOKEN", "dev token is unset!")
req.headers.set("authorization".title(), "Bearer " + token)

monkey.check(
name = "adds_special_headers",
before_request = add_special_headers,
Expand Down
7 changes: 5 additions & 2 deletions fuzzymonkey.star
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ monkey.openapi3(
# Note: references to schemas in `file` are resolved relative to file's location.
file = SPEC,
host = "https://jsonplaceholder.typicode.com",
# header_authorization = "Bearer {}".format(monkey.env("DEV_API_TOKEN")),
)

# Note: exec commands are executed in shells sharing the same environment variables,
Expand Down Expand Up @@ -48,9 +47,13 @@ def add_special_headers(ctx):

assert that(MY_HEADER.title()).is_equal_to(MY_HEADER)
assert that(dict(req.headers)).does_not_contain_key(MY_HEADER)
req.headers.set(MY_HEADER, "value!")
req.headers.add(MY_HEADER, "value!")
print("Added an extra header:", MY_HEADER)

# Let's also set a bearer token:
token = monkey.env("DEV_API_TOKEN", "dev token is unset!")
req.headers.set("authorization".title(), "Bearer " + token)

monkey.check(
name = "adds_special_headers",
before_request = add_special_headers,
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/FuzzyMonkeyCo/monkey

go 1.21

toolchain go1.23.2

require (
github.com/alecthomas/chroma/v2 v2.14.0
github.com/bazelbuild/buildtools v0.0.0-20240918101019-be1c24cc9a44
Expand Down
816 changes: 402 additions & 414 deletions pkg/internal/fm/fuzzymonkey.pb.go

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions pkg/internal/fm/fuzzymonkey.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ message Clt {
message Model {
string name = 1;
message OpenAPIv3 {
// Spec is the spec pointed at by File
SpecIR spec = 1;
// File path within current directory pointing to a YAML/JSON spec
string file = 1;
string file = 2;
// Host superseeds the spec's base URL
string host = 2;
// HeaderAuthorization is added as bearer token if non-empty
string header_authorization = 3;
// Spec is the spec pointed at by File
SpecIR spec = 4;
string host = 3;
}
oneof model {
OpenAPIv3 openapiv3 = 2;
Expand Down
112 changes: 33 additions & 79 deletions pkg/internal/fm/fuzzymonkey_vtproto.pb.go

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

13 changes: 4 additions & 9 deletions pkg/internal/fm/proto.lock
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,18 @@
"fields": [
{
"id": 1,
"name": "file",
"type": "string"
"name": "spec",
"type": "SpecIR"
},
{
"id": 2,
"name": "host",
"name": "file",
"type": "string"
},
{
"id": 3,
"name": "header_authorization",
"name": "host",
"type": "string"
},
{
"id": 4,
"name": "spec",
"type": "SpecIR"
}
]
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/modeler/openapiv3/caller_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ func (m *oa3) buildHTTPRequest(ctx context.Context, msg *fm.Srv_Call) (req *http
}
}

if authz := m.pb.HeaderAuthorization; authz != "" {
r.Header.Add(headerAuthorization, authz)
}

r.Header.Set(headerUserAgent, ctx.Value(ctxvalues.XUserAgent).(string))

if host := m.pb.Host; host != "" {
Expand Down
1 change: 0 additions & 1 deletion pkg/modeler/openapiv3/ir_openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestEncodeVersusEncodeDecodeEncode(t *testing.T) {
m1.fromProto(&m1Prime)
require.NotEmpty(t, m1.pb.File)
require.Empty(t, m1.pb.Host)
require.Empty(t, m1.pb.HeaderAuthorization)
require.NotEmpty(t, m1.pb.Spec)
require.NotNil(t, m1.vald)
validateSomeSchemas(t, m1)
Expand Down
5 changes: 2 additions & 3 deletions pkg/modeler/openapiv3/modeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ func New(kwargs []starlark.Tuple) (modeler.Interface, error) {
m := &oa3{
name: name,
pb: &fm.Clt_Fuzz_Model_OpenAPIv3{
File: lot.file.GoString(),
Host: lot.host.GoString(),
HeaderAuthorization: lot.headerAuthorization.GoString(),
File: lot.file.GoString(),
Host: lot.host.GoString(),
//TODO: tags
},
}
Expand Down

0 comments on commit 4d35c19

Please sign in to comment.