| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- package protocol
- import (
- "errors"
- "log"
- "net/http"
- "net/http/httptest"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- )
- func TestHTTPError_Error(t *testing.T) {
- err := HTTPError{StatusCode: 400, Body: "bad request"}
- assert.Equal(t, "bad request", err.Error())
- }
- func TestHTTPProtocol_BadRequest(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.BadRequest("bad req")
- assert.Equal(t, http.StatusBadRequest, err.StatusCode)
- assert.Equal(t, "bad req", err.Body)
- }
- func TestHTTPProtocol_UnprocessableEntity(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.UnprocessableEntity("")
- assert.Equal(t, http.StatusUnprocessableEntity, err.StatusCode)
- assert.Equal(t, "Unprocessable Entity", err.Body)
- err2 := hp.UnprocessableEntity("custom")
- assert.Equal(t, "custom", err2.Body)
- }
- func TestHTTPProtocol_InternalServerError(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.InternalServerError("")
- assert.Equal(t, http.StatusInternalServerError, err.StatusCode)
- assert.Equal(t, "Internal Server Error", err.Body)
- err2 := hp.InternalServerError("custom")
- assert.Equal(t, "custom", err2.Body)
- }
- func TestHTTPProtocol_NotImplemented(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.NotImplemented("")
- assert.Equal(t, http.StatusNotImplemented, err.StatusCode)
- assert.Equal(t, "Not Implemented", err.Body)
- err2 := hp.NotImplemented("custom")
- assert.Equal(t, "custom", err2.Body)
- }
- func TestHTTPProtocol_Forbidden(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.Forbidden("")
- assert.Equal(t, http.StatusForbidden, err.StatusCode)
- assert.Equal(t, "Forbidden", err.Body)
- err2 := hp.Forbidden("custom")
- assert.Equal(t, "custom", err2.Body)
- }
- func TestHTTPProtocol_NotFound(t *testing.T) {
- hp := HTTPProtocol{}
- err := hp.NotFound()
- assert.Equal(t, http.StatusNotFound, err.StatusCode)
- assert.Equal(t, "Not Found", err.Body)
- }
- func TestHTTPProtocol_ToResponse(t *testing.T) {
- hp := HTTPProtocol{}
- resp := hp.ToResponse("data", nil)
- assert.Equal(t, http.StatusOK, resp.Code)
- assert.Equal(t, "data", resp.Data)
- resp2 := hp.ToResponse("data", errors.New("fail"))
- assert.Equal(t, http.StatusInternalServerError, resp2.Code)
- assert.Equal(t, "fail", resp2.Message)
- }
- func TestHTTPProtocol_WriteRawOK(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteRawOK(rw)
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Equal(t, "application/json", rw.Header().Get("Content-Type"))
- assert.Equal(t, "0", rw.Header().Get("Content-Length"))
- }
- func TestHTTPProtocol_WriteRawNoContent(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteRawNoContent(rw)
- assert.Equal(t, http.StatusNoContent, rw.Code)
- assert.Equal(t, "application/json", rw.Header().Get("Content-Type"))
- }
- func TestHTTPProtocol_WriteJSONData(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteJSONData(rw, map[string]string{"foo": "bar"})
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Contains(t, rw.Body.String(), "foo")
- }
- func TestHTTPProtocol_WriteRawError(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteRawError(rw, http.StatusBadRequest, "bad")
- assert.Equal(t, http.StatusBadRequest, rw.Code)
- assert.Equal(t, "text/plain; charset=utf-8", rw.Header().Get("Content-Type"))
- assert.Contains(t, rw.Body.String(), "bad")
- }
- func TestHTTPProtocol_WriteEncodedError(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteEncodedError(rw, http.StatusBadRequest, map[string]string{"err": "bad"})
- assert.Equal(t, http.StatusBadRequest, rw.Code)
- assert.Contains(t, rw.Body.String(), "bad")
- }
- func TestHTTPProtocol_WriteData(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteData(rw, map[string]string{"foo": "bar"})
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Contains(t, rw.Body.String(), "foo")
- }
- func TestHTTPProtocol_WriteDataWithWarning(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteDataWithWarning(rw, map[string]string{"foo": "bar"}, "warn")
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Contains(t, rw.Body.String(), "warn")
- }
- func TestHTTPProtocol_WriteDataWithMessage(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteDataWithMessage(rw, map[string]string{"foo": "bar"}, "msg")
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Contains(t, rw.Body.String(), "msg")
- }
- func TestHTTPProtocol_WriteDataWithMessageAndWarning(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteDataWithMessageAndWarning(rw, map[string]string{"foo": "bar"}, "msg", "warn")
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Contains(t, rw.Body.String(), "msg")
- assert.Contains(t, rw.Body.String(), "warn")
- }
- func TestHTTPProtocol_WriteError(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- hp.WriteError(rw, HTTPError{StatusCode: 400, Body: "fail"})
- assert.Equal(t, 400, rw.Code)
- body := rw.Body.String()
- log.Println("body: " + body)
- assert.Contains(t, body, "fail")
- }
- func TestHTTPProtocol_WriteResponse(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- resp := &HTTPResponse{Code: 200, Data: "foo"}
- hp.WriteResponse(rw, resp)
- assert.Equal(t, 200, rw.Code)
- assert.Contains(t, rw.Body.String(), "foo")
- }
- func TestHTTPProtocol_WriteData_Structure(t *testing.T) {
- hp := HTTPProtocol{}
- rw := httptest.NewRecorder()
- data := map[string]string{"foo": "bar"}
- hp.WriteData(rw, data)
- assert.Equal(t, http.StatusOK, rw.Code)
- assert.Equal(t, "application/json", rw.Header().Get("Content-Type"))
- // Check the structure of the JSON response
- body := rw.Body.String()
- assert.Contains(t, body, "\"code\":200")
- assert.Contains(t, body, "\"data\":{\"foo\":\"bar\"}")
- assert.NotContains(t, body, "message")
- assert.NotContains(t, body, "warning")
- }
- func TestHTTPProtocol_HTTPResponse(t *testing.T) {
- proto := HTTP()
- // Test data, meta, warning, message
- data := struct {
- Apples int
- Bananas int
- }{
- Apples: 12,
- Bananas: 4,
- }
- meta := map[string]interface{}{
- "lastUpdated": time.Date(2025, time.September, 5, 13, 27, 3, 0, time.UTC),
- }
- warning := "warning"
- message := "message"
- // Test building an HTTPResponse
- var r *HTTPResponse
- r = proto.NewResponse()
- if r == nil || r.Code != 200 {
- t.Errorf("expected code %d, received %d", 200, r.Code)
- }
- r = proto.NewResponse(302).WithMessage("Moved Temporarily")
- if r == nil || r.Code != 302 || r.Message != "Moved Temporarily" {
- t.Errorf("expected %d %s, received %d %s", 302, "Moved Temporarily", r.Code, r.Message)
- }
- r = r.WithCode(200).WithData(data).WithMeta(meta).WithMessage(message).WithWarning(warning)
- if r == nil {
- t.Errorf("unexpected nil response")
- }
- if r.Code != 200 {
- t.Errorf("expected code %d, received %d", 200, r.Code)
- }
- if r.Data == nil {
- t.Error("unexpected nil data")
- }
- if r.Meta == nil {
- t.Error("unexpected nil meta")
- }
- if r.Message == "" {
- t.Error("unexpected empty message")
- }
- if r.Warning == "" {
- t.Error("unexpected empty warning")
- }
- // Test assigning attribtues to nil response
- r = nil
- r = r.WithCode(413)
- if r == nil || r.Code != 413 {
- t.Errorf("expected code %d, received %d", 413, r.Code)
- }
- r = nil
- r = r.WithData(data)
- if r == nil || r.Data == nil {
- t.Error("expected data, received nil")
- }
- r = nil
- r = r.WithMeta(meta)
- if r == nil || r.Meta == nil {
- t.Error("expected meta, received nil")
- }
- r = nil
- r = r.WithWarning(warning)
- if r == nil || r.Warning == "" {
- t.Error("expected warning, received empty string")
- }
- r = nil
- r = r.WithMessage(message)
- if r == nil || r.Message == "" {
- t.Error("expected message, received empty string")
- }
- }
- func TestHTTPProtocol_NewError(t *testing.T) {
- proto := HTTP()
- err := errors.New("error")
- httpErr := proto.NewError(err)
- if httpErr == nil || httpErr.StatusCode != 500 || httpErr.Body != "error" {
- t.Errorf("expected 500 error, received %d %s", httpErr.StatusCode, httpErr.Body)
- }
- httpErr = proto.NewError(err, 400)
- if httpErr == nil || httpErr.StatusCode != 400 || httpErr.Body != "error" {
- t.Errorf("expected 400 error, received %d %s", httpErr.StatusCode, httpErr.Body)
- }
- httpErr = proto.NewError(err, 400, 404)
- if httpErr == nil || httpErr.StatusCode != 400 || httpErr.Body != "error" {
- t.Errorf("expected 400 error, received %d %s", httpErr.StatusCode, httpErr.Body)
- }
- }
|