Skip to content

Commit

Permalink
Fix nilaway for TDR
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Jul 7, 2024
1 parent 4cf2ccf commit 3eefb88
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
17 changes: 11 additions & 6 deletions tdr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ func (c *Client) SetBaseTDRURL(urlStr string) error {
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}

var err error
c.baseTDRURL, err = url.Parse(urlStr)
return err
tdrURL, err := url.Parse(urlStr)
if err != nil {
return err
}
c.baseTDRURL = tdrURL
return nil
}

// newTDRRequest creates an new TDR API request. A relative URL path can be provided in
Expand Down Expand Up @@ -159,7 +161,7 @@ type Response struct {
}

func (r *Response) StatusCode() int {
if r.Response != nil {
if r != nil && r.Response != nil {
return r.Response.StatusCode
}
return 0
Expand All @@ -179,6 +181,9 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
if err != nil {
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("response is nil")
}
defer func() {
_ = resp.Body.Close()
}()
Expand All @@ -192,7 +197,7 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
return response, err
}

if v != nil {
if v != nil && resp.Body != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
} else {
Expand Down
3 changes: 3 additions & 0 deletions tdr/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ func TestDebug(t *testing.T) {
if err != nil {
t.Fatalf("Error: %v", err)
}
if serverTDR == nil {
t.Fatal("No serverTDR")
}

tdrClient, err = NewClient(iamClient, &Config{
TDRURL: serverTDR.URL,
Expand Down
8 changes: 8 additions & 0 deletions tdr/contracts_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ func TestGetContract(t *testing.T) {
teardown := setup(t)
defer teardown()

if tdrClient == nil {
t.Fatal("Expected tdrClient to be set")
}

muxTDR.HandleFunc("/store/tdr/Contract", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("dataType") == "" {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -126,6 +130,10 @@ func TestCreateContract(t *testing.T) {
teardown := setup(t)
defer teardown()

if tdrClient == nil {
t.Fatal("Expected tdrClient to be set")
}

muxTDR.HandleFunc("/store/tdr/Contract", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions tdr/data_items_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func TestGetDataItem(t *testing.T) {
teardown := setup(t)
defer teardown()

if tdrClient == nil {
t.Fatal("Expected tdrClient to be set")
}

muxTDR.HandleFunc("/store/tdr/DataItem", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("organization") == "" {
w.WriteHeader(http.StatusBadRequest)
Expand Down

0 comments on commit 3eefb88

Please sign in to comment.