-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(proxy): empty response body should skip writing a response file.
- Loading branch information
1 parent
4f3602d
commit 8114b07
Showing
2 changed files
with
133 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package proxy | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func Test_getResponseFile(t *testing.T) { | ||
outputDir, err := os.MkdirTemp(os.TempDir(), "imposter-cli") | ||
if err != nil { | ||
panic(err) | ||
} | ||
rootUrl, _ := url.Parse("https://example.com") | ||
|
||
responseBody := []byte("test") | ||
bodyHash := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" | ||
|
||
type args struct { | ||
upstreamHost string | ||
dir string | ||
options RecorderOptions | ||
exchange HttpExchange | ||
fileHashes *map[string]string | ||
prefix string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty response body", | ||
args: args{ | ||
upstreamHost: "example.com", | ||
dir: outputDir, | ||
options: RecorderOptions{FlatResponseFileStructure: false}, | ||
exchange: HttpExchange{ | ||
Request: &http.Request{Method: "GET", URL: rootUrl}, | ||
ResponseBody: &[]byte{}, | ||
}, | ||
fileHashes: buildMap(outputDir, []string{}), | ||
}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "existing response body", | ||
args: args{ | ||
upstreamHost: "example.com", | ||
dir: outputDir, | ||
options: RecorderOptions{FlatResponseFileStructure: false}, | ||
exchange: HttpExchange{ | ||
Request: &http.Request{Method: "GET", URL: rootUrl}, | ||
ResponseBody: &responseBody, | ||
ResponseHeaders: &http.Header{}, | ||
}, | ||
fileHashes: buildMap(outputDir, []string{bodyHash}), | ||
}, | ||
want: path.Join(outputDir, "existing-file-0.txt"), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "new response body", | ||
args: args{ | ||
upstreamHost: "example.com", | ||
dir: outputDir, | ||
options: RecorderOptions{FlatResponseFileStructure: false}, | ||
exchange: HttpExchange{ | ||
Request: &http.Request{Method: "GET", URL: rootUrl}, | ||
ResponseBody: &responseBody, | ||
ResponseHeaders: &http.Header{}, | ||
}, | ||
fileHashes: buildMap(outputDir, []string{}), | ||
}, | ||
want: path.Join(outputDir, "GET-index.txt"), | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := getResponseFile(tt.args.upstreamHost, tt.args.dir, tt.args.options, tt.args.exchange, tt.args.fileHashes, tt.args.prefix) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("getResponseFile() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("getResponseFile() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func buildMap(dir string, hashes []string) *map[string]string { | ||
m := make(map[string]string) | ||
for i, hash := range hashes { | ||
filename := fmt.Sprintf("existing-file-%d.txt", i) | ||
m[hash] = path.Join(dir, filename) | ||
} | ||
return &m | ||
} |