Skip to content

Commit

Permalink
Merge pull request #84 from fabiante/feat/refactor-http-driver
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiante authored Sep 23, 2023
2 parents 113f001 + 310c1d5 commit f1165d3
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions tests/driver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"

Expand All @@ -29,8 +30,22 @@ func NewHTTPDriver(basePath string, transport http.RoundTripper) *HTTPDriver {
return &HTTPDriver{BasePath: basePath, Client: client}
}

func (driver *HTTPDriver) newRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}

return req, nil
}

func (driver *HTTPDriver) ResolvePURL(domain string, name string) (*url.URL, error) {
response, err := driver.Client.Get(fmt.Sprintf("%s/r/%s/%s", driver.BasePath, domain, name))
req, err := driver.newRequest(http.MethodGet, fmt.Sprintf("%s/r/%s/%s", driver.BasePath, domain, name), nil)
if err != nil {
return nil, err
}

response, err := driver.Client.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -60,7 +75,7 @@ func (driver *HTTPDriver) SavePURL(purl *dsl.PURL) (string, error) {
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s/a/domains/%s/purls/%s", driver.BasePath, purl.Domain, purl.Name), body)
req, err := driver.newRequest(http.MethodPut, fmt.Sprintf("%s/a/domains/%s/purls/%s", driver.BasePath, purl.Domain, purl.Name), body)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -88,7 +103,7 @@ func (driver *HTTPDriver) SavePURL(purl *dsl.PURL) (string, error) {
}

func (driver *HTTPDriver) CreateDomain(name string) error {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/a/domains/%s", driver.BasePath, name), nil)
req, err := driver.newRequest(http.MethodPost, fmt.Sprintf("%s/a/domains/%s", driver.BasePath, name), nil)
if err != nil {
return err
}
Expand Down

0 comments on commit f1165d3

Please sign in to comment.