Skip to content

Commit d8fd4e8

Browse files
authored
chore(curl): Add nil pointer checks and tests for the outputResult functions (#620)
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent ddfb512 commit d8fd4e8

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

docs/stackit_curl.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ stackit curl URL [flags]
1717
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones
1818
1919
Get all the DNS zones for project with ID xxx via GET request to https://dns.api.stackit.cloud/v1/projects/xxx/zones, write complete response (headers and body) to file "./output.txt"
20-
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -include --output ./output.txt
20+
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones --include --output ./output.txt
2121
2222
Create a new DNS zone for project with ID xxx via POST request to https://dns.api.stackit.cloud/v1/projects/xxx/zones with payload from file "./payload.json"
2323
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -X POST --data @./payload.json

internal/cmd/curl/curl.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5656
),
5757
examples.NewExample(
5858
`Get all the DNS zones for project with ID xxx via GET request to https://dns.api.stackit.cloud/v1/projects/xxx/zones, write complete response (headers and body) to file "./output.txt"`,
59-
"$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -include --output ./output.txt",
59+
"$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones --include --output ./output.txt",
6060
),
6161
examples.NewExample(
6262
`Create a new DNS zone for project with ID xxx via POST request to https://dns.api.stackit.cloud/v1/projects/xxx/zones with payload from file "./payload.json"`,
@@ -198,6 +198,9 @@ func buildRequest(model *inputModel, bearerToken string) (*http.Request, error)
198198
}
199199

200200
func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) error {
201+
if resp == nil {
202+
return fmt.Errorf("http response is empty")
203+
}
201204
output := make([]byte, 0)
202205
if model.IncludeResponseHeaders {
203206
respHeader, err := httputil.DumpResponse(resp, false)
@@ -215,7 +218,7 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
215218
if model.OutputFile == nil {
216219
p.Outputln(string(output))
217220
} else {
218-
err = os.WriteFile(*model.OutputFile, output, 0o600)
221+
err = os.WriteFile(utils.PtrString(model.OutputFile), output, 0o600)
219222
if err != nil {
220223
return fmt.Errorf("write output to file: %w", err)
221224
}

internal/cmd/curl/curl_test.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import (
55
"context"
66
"fmt"
77
"net/http"
8+
"os"
89
"testing"
910

11+
"github.com/google/go-cmp/cmp"
12+
"github.com/google/go-cmp/cmp/cmpopts"
1013
"github.com/spf13/viper"
1114
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
1215
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1316
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1417
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
15-
16-
"github.com/google/go-cmp/cmp"
17-
"github.com/google/go-cmp/cmp/cmpopts"
1818
)
1919

2020
var testURL = "https://some-service.api.stackit.cloud/v1/foo?bar=baz"
@@ -37,7 +37,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
3737
dataFlag: "data",
3838
includeResponseHeadersFlag: "true",
3939
failOnHTTPErrorFlag: "true",
40-
outputFileFlag: "path/to/output.txt",
40+
outputFileFlag: "./output.txt",
4141
}
4242
for _, mod := range mods {
4343
mod(flagValues)
@@ -53,7 +53,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
5353
Data: utils.Ptr("data"),
5454
IncludeResponseHeaders: true,
5555
FailOnHTTPError: true,
56-
OutputFile: utils.Ptr("path/to/output.txt"),
56+
OutputFile: utils.Ptr("./output.txt"),
5757
}
5858
for _, mod := range mods {
5959
mod(model)
@@ -403,3 +403,45 @@ func TestBuildRequest(t *testing.T) {
403403
})
404404
}
405405
}
406+
407+
func TestOutputResponse(t *testing.T) {
408+
type args struct {
409+
model *inputModel
410+
resp *http.Response
411+
}
412+
tests := []struct {
413+
name string
414+
args args
415+
wantErr bool
416+
}{
417+
{
418+
name: "empty",
419+
args: args{},
420+
wantErr: true,
421+
},
422+
{
423+
name: "http response as argument",
424+
args: args{
425+
model: fixtureInputModel(),
426+
resp: &http.Response{Body: http.NoBody},
427+
},
428+
wantErr: false,
429+
},
430+
}
431+
p := print.NewPrinter()
432+
p.Cmd = NewCmd(p)
433+
for _, tt := range tests {
434+
t.Run(tt.name, func(t *testing.T) {
435+
if err := outputResponse(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr {
436+
t.Errorf("outputResponse() error = %v, wantErr %v", err, tt.wantErr)
437+
}
438+
if tt.args.model != nil {
439+
if _, err := os.Stat(*tt.args.model.OutputFile); err == nil {
440+
if err := os.Remove(*tt.args.model.OutputFile); err != nil {
441+
t.Errorf("remove output file error = %v", err)
442+
}
443+
}
444+
}
445+
})
446+
}
447+
}

0 commit comments

Comments
 (0)