Skip to content

chore(curl): Add nil pointer checks and tests for the outputResult fu… #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/stackit_curl.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ stackit curl URL [flags]
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones

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"
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -include --output ./output.txt
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones --include --output ./output.txt

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"
$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -X POST --data @./payload.json
Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/curl/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
),
examples.NewExample(
`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"`,
"$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones -include --output ./output.txt",
"$ stackit curl https://dns.api.stackit.cloud/v1/projects/xxx/zones --include --output ./output.txt",
),
examples.NewExample(
`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"`,
Expand Down Expand Up @@ -198,6 +198,9 @@ func buildRequest(model *inputModel, bearerToken string) (*http.Request, error)
}

func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) error {
if resp == nil {
return fmt.Errorf("http response is empty")
}
output := make([]byte, 0)
if model.IncludeResponseHeaders {
respHeader, err := httputil.DumpResponse(resp, false)
Expand All @@ -215,7 +218,7 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
if model.OutputFile == nil {
p.Outputln(string(output))
} else {
err = os.WriteFile(*model.OutputFile, output, 0o600)
err = os.WriteFile(utils.PtrString(model.OutputFile), output, 0o600)
if err != nil {
return fmt.Errorf("write output to file: %w", err)
}
Expand Down
52 changes: 47 additions & 5 deletions internal/cmd/curl/curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"context"
"fmt"
"net/http"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

var testURL = "https://some-service.api.stackit.cloud/v1/foo?bar=baz"
Expand All @@ -37,7 +37,7 @@ func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]st
dataFlag: "data",
includeResponseHeadersFlag: "true",
failOnHTTPErrorFlag: "true",
outputFileFlag: "path/to/output.txt",
outputFileFlag: "./output.txt",
}
for _, mod := range mods {
mod(flagValues)
Expand All @@ -53,7 +53,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
Data: utils.Ptr("data"),
IncludeResponseHeaders: true,
FailOnHTTPError: true,
OutputFile: utils.Ptr("path/to/output.txt"),
OutputFile: utils.Ptr("./output.txt"),
}
for _, mod := range mods {
mod(model)
Expand Down Expand Up @@ -403,3 +403,45 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResponse(t *testing.T) {
type args struct {
model *inputModel
resp *http.Response
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "http response as argument",
args: args{
model: fixtureInputModel(),
resp: &http.Response{Body: http.NoBody},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResponse(p, tt.args.model, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResponse() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.args.model != nil {
if _, err := os.Stat(*tt.args.model.OutputFile); err == nil {
if err := os.Remove(*tt.args.model.OutputFile); err != nil {
t.Errorf("remove output file error = %v", err)
}
}
}
})
}
}
Loading