diff --git a/pkg/httpassert/README.md b/pkg/httpassert/README.md index c8d0eb0..4da4918 100644 --- a/pkg/httpassert/README.md +++ b/pkg/httpassert/README.md @@ -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>) @@ -30,7 +32,7 @@ const IgnoreJsonValue = "" ``` -## type [Extractor]() +## type [Extractor]() @@ -38,8 +40,17 @@ const IgnoreJsonValue = "" type Extractor func(t *testing.T, actual any) any ``` + +### func [ExtractRegexTo]() + +```go +func ExtractRegexTo(value string, ptr any) Extractor +``` + + + -### func [ExtractTo]() +### func [ExtractTo]() ```go func ExtractTo(ptr any) Extractor @@ -53,7 +64,7 @@ request.Expect().JsonPath("$.data.id", httpassert.ExtractTo(&id)) ``` -## type [Matcher]() +## type [Matcher]() @@ -62,7 +73,7 @@ type Matcher func(t *testing.T, actual any) bool ``` -### func [Contains]() +### func [Contains]() ```go func Contains(v string) Matcher @@ -71,7 +82,7 @@ func Contains(v string) Matcher Contains checks if a string contains the value Example: ExpectJsonPath\("$.data.name", httpassert.Contains\("foo"\)\) -### func [HasSize]() +### func [HasSize]() ```go func HasSize(e int) Matcher @@ -79,6 +90,15 @@ func HasSize(e int) Matcher HasSize checks the length of arrays, maps, or strings. Example: ExpectJsonPath\("$.data", httpassert.HasSize\(11\)\) + +### func [Regex]() + +```go +func Regex(expr string) Matcher +``` + +Regex checks if a string matches the given regular expression Example: ExpectJsonPath\("$.data.name", httpassert.Regex\("^foo.\*bar$"\)\) + ## type [Request]() @@ -130,7 +150,7 @@ func New(t *testing.T, router http.Handler) Request New returns a new Request instance for the given router. -## type [Response]() +## type [Response]() nolint:interfacebloat Response interface provides fluent response assertions. @@ -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 diff --git a/pkg/httpassert/request.go b/pkg/httpassert/request.go index cfd45d6..260a71f 100644 --- a/pkg/httpassert/request.go +++ b/pkg/httpassert/request.go @@ -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) @@ -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) diff --git a/pkg/httpassert/request_test.go b/pkg/httpassert/request_test.go index c28a133..867672e 100644 --- a/pkg/httpassert/request_test.go +++ b/pkg/httpassert/request_test.go @@ -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 {