Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions pkg/httpassert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import "github.com/greenbone/opensight-golang-libraries/pkg/httpassert"

- [Constants](<#constants>)
- [type Extractor](<#Extractor>)
- [func ExtractRegexTo\(value string, ptr any\) Extractor](<#ExtractRegexTo>)
- [func ExtractTo\(ptr any\) Extractor](<#ExtractTo>)
- [type Matcher](<#Matcher>)
- [func Contains\(v string\) Matcher](<#Contains>)
- [func HasSize\(e int\) Matcher](<#HasSize>)
- [func Regex\(expr string\) Matcher](<#Regex>)
- [type Request](<#Request>)
- [func New\(t \*testing.T, router http.Handler\) Request](<#New>)
- [type Response](<#Response>)
Expand All @@ -30,16 +32,25 @@ const IgnoreJsonValue = "<IGNORE>"
```

<a name="Extractor"></a>
## type [Extractor](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L15>)
## type [Extractor](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L16>)



```go
type Extractor func(t *testing.T, actual any) any
```

<a name="ExtractRegexTo"></a>
### func [ExtractRegexTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L29>)

```go
func ExtractRegexTo(value string, ptr any) Extractor
```



<a name="ExtractTo"></a>
### func [ExtractTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L22>)
### func [ExtractTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L23>)

```go
func ExtractTo(ptr any) Extractor
Expand All @@ -53,7 +64,7 @@ request.Expect().JsonPath("$.data.id", httpassert.ExtractTo(&id))
```

<a name="Matcher"></a>
## type [Matcher](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L14>)
## type [Matcher](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L15>)



Expand All @@ -62,7 +73,7 @@ type Matcher func(t *testing.T, actual any) bool
```

<a name="Contains"></a>
### func [Contains](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L42>)
### func [Contains](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L43>)

```go
func Contains(v string) Matcher
Expand All @@ -71,14 +82,23 @@ func Contains(v string) Matcher
Contains checks if a string contains the value Example: ExpectJsonPath\("$.data.name", httpassert.Contains\("foo"\)\)

<a name="HasSize"></a>
### func [HasSize](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L18>)
### func [HasSize](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L19>)

```go
func HasSize(e int) Matcher
```

HasSize checks the length of arrays, maps, or strings. Example: ExpectJsonPath\("$.data", httpassert.HasSize\(11\)\)

<a name="Regex"></a>
### func [Regex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L52>)

```go
func Regex(expr string) Matcher
```

Regex checks if a string matches the given regular expression Example: ExpectJsonPath\("$.data.name", httpassert.Regex\("^foo.\*bar$"\)\)

<a name="Request"></a>
## type [Request](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/request.go#L23-L55>)

Expand Down Expand Up @@ -130,7 +150,7 @@ func New(t *testing.T, router http.Handler) Request
New returns a new Request instance for the given router.

<a name="Response"></a>
## type [Response](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/response.go#L24-L44>)
## type [Response](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/response.go#L24-L46>)

nolint:interfacebloat Response interface provides fluent response assertions.

Expand All @@ -150,6 +170,8 @@ type Response interface {
JsonTemplateFile(path string, values map[string]any) Response
JsonFile(path string) Response

Header(name string, value any) Response

Body(body string) Response
GetJsonBodyObject(target any) Response
GetBody() string
Expand Down
24 changes: 12 additions & 12 deletions pkg/httpassert/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ type request struct {

func (m *request) Perform(verb string, path string) Request {
switch verb {
case "Post":
case "Post", http.MethodPost:
return m.Post(path)
case "Put":
case "Put", http.MethodPut:
return m.Put(path)
case "Get":
case "Get", http.MethodGet:
return m.Get(path)
case "Delete":
case "Delete", http.MethodDelete:
return m.Delete(path)
case "Options":
case "Options", http.MethodOptions:
return m.Options(path)
case "Patch":
case "Patch", http.MethodPatch:
return m.Patch(path)
default:
m.t.Fatalf("unknown verb: %s", verb)
Expand All @@ -90,17 +90,17 @@ func (m *request) Perform(verb string, path string) Request {

func (m *request) Performf(verb string, path string, a ...interface{}) Request {
switch verb {
case "Post":
case "Post", http.MethodPost:
return m.Postf(path, a...)
case "Put":
case "Put", http.MethodPut:
return m.Putf(path, a...)
case "Get":
case "Get", http.MethodGet:
return m.Getf(path, a...)
case "Delete":
case "Delete", http.MethodDelete:
return m.Deletef(path, a...)
case "Options":
case "Options", http.MethodOptions:
return m.Optionsf(path, a...)
case "Patch":
case "Patch", http.MethodPatch:
return m.Patchf(path, a...)
default:
m.t.Fatalf("unknown verb: %s", verb)
Expand Down
17 changes: 17 additions & 0 deletions pkg/httpassert/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ func TestRequestPerform(t *testing.T) {
"Options": {
verb: "Options",
},
"Get (all caps)": {
verb: http.MethodGet,
},
"Post (all caps)": {
verb: http.MethodPost,
content: `{}`,
},
"Put (all caps)": {
verb: http.MethodPut,
content: `{}`,
},
"Delete (all caps)": {
verb: http.MethodDelete,
},
"Options (all caps)": {
verb: http.MethodOptions,
},
}

for name, tc := range tests {
Expand Down
Loading