// Copyright 2021 Harness Inc. All rights reserved. // Use of this source code is governed by the Polyform Free Trial License // that can be found in the LICENSE.md file for this repository. package render import ( "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestWriteError(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") InternalError(w, err) if got, want := w.Code, 500; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteErrorf(t *testing.T) { w := httptest.NewRecorder() InternalErrorf(w, "pc load letter") if got, want := w.Code, 500; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, "pc load letter"; got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteErrorCode(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") ErrorCode(w, err, 418) if got, want := w.Code, 418; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteNotFound(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") NotFound(w, err) if got, want := w.Code, 404; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteNotFoundf(t *testing.T) { w := httptest.NewRecorder() NotFoundf(w, "pc load letter") if got, want := w.Code, 404; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, "pc load letter"; got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteUnauthorized(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") Unauthorized(w, err) if got, want := w.Code, 401; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteForbidden(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") Forbidden(w, err) if got, want := w.Code, 403; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteBadRequest(t *testing.T) { w := httptest.NewRecorder() err := New("pc load letter") BadRequest(w, err) if got, want := w.Code, 400; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, err.Error(); got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteBadRequestf(t *testing.T) { w := httptest.NewRecorder() BadRequestf(w, "pc load letter") if got, want := w.Code, 400; want != got { t.Errorf("Want response code %d, got %d", want, got) } errjson := &Error{} json.NewDecoder(w.Body).Decode(errjson) if got, want := errjson.Message, "pc load letter"; got != want { t.Errorf("Want error message %s, got %s", want, got) } } func TestWriteJSON(t *testing.T) { // without indent { w := httptest.NewRecorder() JSON(w, map[string]string{"hello": "world"}, http.StatusTeapot) if got, want := w.Body.String(), "{\"hello\":\"world\"}\n"; got != want { t.Errorf("Want JSON body %q, got %q", want, got) } if got, want := w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8"; got != want { t.Errorf("Want Content-Type %q, got %q", want, got) } if got, want := w.Code, http.StatusTeapot; got != want { t.Errorf("Want status code %d, got %d", want, got) } } // with indent { indent = true defer func() { indent = false }() w := httptest.NewRecorder() JSON(w, map[string]string{"hello": "world"}, http.StatusTeapot) if got, want := w.Body.String(), "{\n \"hello\": \"world\"\n}\n"; got != want { t.Errorf("Want JSON body %q, got %q", want, got) } } }