Fix CodeClimate duplication smells

by extracting one http helper method and
increasing the threshold for duplication issues.
This commit is contained in:
Maximilian Paß
2022-05-29 21:41:24 +02:00
committed by Sebastian Serth
parent 7ac08b83d0
commit 97a2311a74
2 changed files with 25 additions and 10 deletions

View File

@ -1,5 +1,13 @@
version: "2" version: "2"
checks:
similar-code:
config:
threshold: 110 # Golang default: 100
identical-code:
config:
threshold: 110 # Golang default: 100
plugins: plugins:
golint: golint:
enabled: false enabled: false
@ -10,4 +18,4 @@ plugins:
exclude_patterns: exclude_patterns:
- "**/*_mock.go" - "**/*_mock.go"
- "**/*_test.go" - "**/*_test.go"

View File

@ -117,13 +117,22 @@ func StartTLSServer(t *testing.T, router *mux.Router) (*httptest.Server, error)
return server, nil return server, nil
} }
// HTTPDelete sends a Delete Http Request with body to the passed url. // httpRequest deduplicates the comment and error message wrapping the http.NewRequest call.
func HTTPDelete(url string, body io.Reader) (response *http.Response, err error) { func httpRequest(method, url string, body io.Reader) (*http.Request, error) {
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests //nolint:noctx // we don't need a http.NewRequestWithContext in our tests
req, err := http.NewRequest(http.MethodDelete, url, body) req, err := http.NewRequest(method, url, body)
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating request: %w", err) return nil, fmt.Errorf("error creating request: %w", err)
} }
return req, nil
}
// HTTPDelete sends a "Delete" Http Request with body to the passed url.
func HTTPDelete(url string, body io.Reader) (response *http.Response, err error) {
req, err := httpRequest(http.MethodDelete, url, body)
if err != nil {
return nil, err
}
client := &http.Client{} client := &http.Client{}
response, err = client.Do(req) response, err = client.Do(req)
if err != nil { if err != nil {
@ -134,10 +143,9 @@ func HTTPDelete(url string, body io.Reader) (response *http.Response, err error)
// HTTPPatch sends a Patch Http Request with body to the passed url. // HTTPPatch sends a Patch Http Request with body to the passed url.
func HTTPPatch(url, contentType string, body io.Reader) (response *http.Response, err error) { func HTTPPatch(url, contentType string, body io.Reader) (response *http.Response, err error) {
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests req, err := httpRequest(http.MethodPatch, url, body)
req, err := http.NewRequest(http.MethodPatch, url, body)
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating new request: %w", err) return nil, err
} }
req.Header.Set("Content-Type", contentType) req.Header.Set("Content-Type", contentType)
client := &http.Client{} client := &http.Client{}
@ -149,10 +157,9 @@ func HTTPPatch(url, contentType string, body io.Reader) (response *http.Response
} }
func HTTPPut(url string, body io.Reader) (response *http.Response, err error) { func HTTPPut(url string, body io.Reader) (response *http.Response, err error) {
//nolint:noctx // we don't need a http.NewRequestWithContext in our tests req, err := httpRequest(http.MethodPut, url, body)
req, err := http.NewRequest(http.MethodPut, url, body)
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating new request: %w", err) return nil, err
} }
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)