Skip to content

Commit 457d64f

Browse files
committed
add: httpassert - support http method consts in Perform
When using `Perform` it is common to use as verb value the constants defined in the go std lib `http` package. Unlike the current implementation the verbs are all uppercase there. So support that additionally for added convenience.
1 parent d622c8a commit 457d64f

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

pkg/httpassert/README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import "github.com/greenbone/opensight-golang-libraries/pkg/httpassert"
1212

1313
- [Constants](<#constants>)
1414
- [type Extractor](<#Extractor>)
15+
- [func ExtractRegexTo\(value string, ptr any\) Extractor](<#ExtractRegexTo>)
1516
- [func ExtractTo\(ptr any\) Extractor](<#ExtractTo>)
1617
- [type Matcher](<#Matcher>)
1718
- [func Contains\(v string\) Matcher](<#Contains>)
1819
- [func HasSize\(e int\) Matcher](<#HasSize>)
20+
- [func Regex\(expr string\) Matcher](<#Regex>)
1921
- [type Request](<#Request>)
2022
- [func New\(t \*testing.T, router http.Handler\) Request](<#New>)
2123
- [type Response](<#Response>)
@@ -30,16 +32,25 @@ const IgnoreJsonValue = "<IGNORE>"
3032
```
3133

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

3537

3638

3739
```go
3840
type Extractor func(t *testing.T, actual any) any
3941
```
4042

43+
<a name="ExtractRegexTo"></a>
44+
### func [ExtractRegexTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L29>)
45+
46+
```go
47+
func ExtractRegexTo(value string, ptr any) Extractor
48+
```
49+
50+
51+
4152
<a name="ExtractTo"></a>
42-
### func [ExtractTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L22>)
53+
### func [ExtractTo](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/extracter.go#L23>)
4354

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

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

5869

5970

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

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

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

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

7687
```go
7788
func HasSize(e int) Matcher
7889
```
7990

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

93+
<a name="Regex"></a>
94+
### func [Regex](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/matcher.go#L52>)
95+
96+
```go
97+
func Regex(expr string) Matcher
98+
```
99+
100+
Regex checks if a string matches the given regular expression Example: ExpectJsonPath\("$.data.name", httpassert.Regex\("^foo.\*bar$"\)\)
101+
82102
<a name="Request"></a>
83103
## type [Request](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/httpassert/request.go#L23-L55>)
84104

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

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

135155
nolint:interfacebloat Response interface provides fluent response assertions.
136156

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

173+
Header(name string, value any) Response
174+
153175
Body(body string) Response
154176
GetJsonBodyObject(target any) Response
155177
GetBody() string

pkg/httpassert/request.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ type request struct {
7070

7171
func (m *request) Perform(verb string, path string) Request {
7272
switch verb {
73-
case "Post":
73+
case "Post", http.MethodPost:
7474
return m.Post(path)
75-
case "Put":
75+
case "Put", http.MethodPut:
7676
return m.Put(path)
77-
case "Get":
77+
case "Get", http.MethodGet:
7878
return m.Get(path)
79-
case "Delete":
79+
case "Delete", http.MethodDelete:
8080
return m.Delete(path)
81-
case "Options":
81+
case "Options", http.MethodOptions:
8282
return m.Options(path)
83-
case "Patch":
83+
case "Patch", http.MethodPatch:
8484
return m.Patch(path)
8585
default:
8686
m.t.Fatalf("unknown verb: %s", verb)
@@ -90,17 +90,17 @@ func (m *request) Perform(verb string, path string) Request {
9090

9191
func (m *request) Performf(verb string, path string, a ...interface{}) Request {
9292
switch verb {
93-
case "Post":
93+
case "Post", http.MethodPost:
9494
return m.Postf(path, a...)
95-
case "Put":
95+
case "Put", http.MethodPut:
9696
return m.Putf(path, a...)
97-
case "Get":
97+
case "Get", http.MethodGet:
9898
return m.Getf(path, a...)
99-
case "Delete":
99+
case "Delete", http.MethodDelete:
100100
return m.Deletef(path, a...)
101-
case "Options":
101+
case "Options", http.MethodOptions:
102102
return m.Optionsf(path, a...)
103-
case "Patch":
103+
case "Patch", http.MethodPatch:
104104
return m.Patchf(path, a...)
105105
default:
106106
m.t.Fatalf("unknown verb: %s", verb)

pkg/httpassert/request_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ func TestRequestPerform(t *testing.T) {
6565
"Options": {
6666
verb: "Options",
6767
},
68+
"Get (all caps)": {
69+
verb: http.MethodGet,
70+
},
71+
"Post (all caps)": {
72+
verb: http.MethodPost,
73+
content: `{}`,
74+
},
75+
"Put (all caps)": {
76+
verb: http.MethodPut,
77+
content: `{}`,
78+
},
79+
"Delete (all caps)": {
80+
verb: http.MethodDelete,
81+
},
82+
"Options (all caps)": {
83+
verb: http.MethodOptions,
84+
},
6885
}
6986

7087
for name, tc := range tests {

0 commit comments

Comments
 (0)