diff --git a/cmd/imageproxy-sign/main_test.go b/cmd/imageproxy-sign/main_test.go index ff03cbd..d9a6df6 100644 --- a/cmd/imageproxy-sign/main_test.go +++ b/cmd/imageproxy-sign/main_test.go @@ -10,6 +10,30 @@ import ( var key = "secret" +func TestMainFunc(t *testing.T) { + os.Args = []string{"imageproxy-sign", "-key", key, "http://example.com/#0x0"} + r, w, err := os.Pipe() + if err != nil { + t.Errorf("error creating pipe: %v", err) + } + defer r.Close() + os.Stdout = w + + main() + w.Close() + + output, err := ioutil.ReadAll(r) + got := string(output) + if err != nil { + t.Errorf("error reading from pipe: %v", err) + } + + want := "url: http://example.com/#0x0\nsignature: pwlnJ3bVazxg2nQxClimqT0VnNxUm5W0cdyg1HpKUPY=\n" + if got != want { + t.Errorf("main output %q, want %q", got, want) + } +} + func TestSign(t *testing.T) { s := "http://example.com/image.jpg#0x0" @@ -36,6 +60,25 @@ func TestSign_URLOnly(t *testing.T) { } } +func TestSign_Errors(t *testing.T) { + var err error + + tests := []struct { + key, url string + }{ + {"", ""}, + {"", "%"}, + {"@/does/not/exist", "s"}, + } + + for _, tt := range tests { + _, err = sign(tt.key, tt.url, false) + if err == nil { + t.Errorf("sign(%q, %q, false) did not return expected error", tt.key, tt.url) + } + } +} + func TestParseKey(t *testing.T) { k, err := parseKey(key) got := string(k)