diff --git a/README.md b/README.md index 7555eafcd843df3c2fb4619af678c248bc9388c1..b849b1b1fbfb5f0efacfc358625835785d4fdd8e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A reasonably complete and well-tested golang port of [Kenneth Reitz][kr]'s [](https://godoc.org/github.com/mccutchen/go-httpbin) [](http://travis-ci.org/mccutchen/go-httpbin) -[](http://gocover.io/github.com/mccutchen/go-httpbin/httpbin) +[](https://codecov.io/gh/mccutchen/go-httpbin) ## Usage diff --git a/httpbin/handlers.go b/httpbin/handlers.go index e6738c61c09cbb9954057ff6fb1e1cdc798e1217..5828599b046eb2aedadbad7158543d7c546b54c2 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -898,13 +898,8 @@ func (h *HTTPBin) DigestAuth(w http.ResponseWriter, r *http.Request) { // UUID - responds with a generated UUID func (h *HTTPBin) UUID(w http.ResponseWriter, r *http.Request) { - u, err := uuidv4() - if err != nil { - http.Error(w, fmt.Sprintf("Failed to generate uuid: %s", err), http.StatusInternalServerError) - return - } resp, _ := json.Marshal(&uuidResponse{ - UUID: u, + UUID: uuidv4(), }) writeJSON(w, resp, http.StatusOK) } diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 3e2606193754bc6a20548e54223d62b985ec70e0..e554695e8b10de4fe785738cf387b8b9f49a281f 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -54,7 +54,7 @@ func assertContentType(t *testing.T, w *httptest.ResponseRecorder, contentType s func assertBodyContains(t *testing.T, w *httptest.ResponseRecorder, needle string) { if !strings.Contains(w.Body.String(), needle) { - t.Fatalf("expected string %v in body", needle) + t.Fatalf("expected string %q in body %q", needle, w.Body.String()) } } @@ -2254,6 +2254,22 @@ func TestBase64(t *testing.T) { "/base64/decode/" + randStringBytes(Base64MaxLen+1), "Cannot handle input", }, + { + "/base64/", + "No input data", + }, + { + "/base64/decode/", + "No input data", + }, + { + "/base64/decode/dmFsaWRfYmFzZTY0X2VuY29kZWRfc3RyaW5n/extra", + "Invalid URL", + }, + { + "/base64/unknown/dmFsaWRfYmFzZTY0X2VuY29kZWRfc3RyaW5n", + "Invalid operation: unknown", + }, } for _, test := range errorTests { diff --git a/httpbin/helpers.go b/httpbin/helpers.go index 87acfbb1b45bfcbaf9397a57ddb970b97c331c07..4c4c66c1a587137a285962fe94433a548a90add2 100644 --- a/httpbin/helpers.go +++ b/httpbin/helpers.go @@ -236,16 +236,15 @@ func sha1hash(input string) string { return fmt.Sprintf("%x", h.Sum([]byte(input))) } -func uuidv4() (string, error) { +func uuidv4() string { buff := make([]byte, 16) _, err := rand.Read(buff[:]) if err != nil { - return "", err + panic(err) } buff[6] = (buff[6] & 0x0f) | 0x40 // Version 4 buff[8] = (buff[8] & 0x3f) | 0x80 // Variant 10 - uuid := fmt.Sprintf("%x-%x-%x-%x-%x", buff[0:4], buff[4:6], buff[6:8], buff[8:10], buff[10:]) - return uuid, nil + return fmt.Sprintf("%x-%x-%x-%x-%x", buff[0:4], buff[4:6], buff[6:8], buff[8:10], buff[10:]) } // base64Helper - describes the base64 operation (encode|decode) and input data