Browse Source

Test new HTTPResponse functions

Signed-off-by: Niko Kovacevic <nikovacevic@gmail.com>
Niko Kovacevic 8 months ago
parent
commit
fc1f0fcb9f
2 changed files with 131 additions and 19 deletions
  1. 43 18
      core/pkg/protocol/http.go
  2. 88 1
      core/pkg/protocol/http_test.go

+ 43 - 18
core/pkg/protocol/http.go

@@ -290,36 +290,61 @@ func (hp HTTPProtocol) NewError(statusCode int, err error) *HTTPError {
 }
 
 func (hp HTTPProtocol) NewResponse(code ...int) *HTTPResponse {
-	hr := &HTTPResponse{Code: http.StatusOK}
+	r := &HTTPResponse{Code: http.StatusOK}
 
 	if len(code) == 1 {
-		hr.Code = code[0]
+		r.Code = code[0]
 	}
 
-	return hr
+	return r
 }
 
-func (hr *HTTPResponse) WithCode(code int) *HTTPResponse {
-	hr.Code = code
-	return hr
+func (r *HTTPResponse) WithCode(code int) *HTTPResponse {
+	if r == nil {
+		r = &HTTPResponse{}
+	}
+
+	r.Code = code
+
+	return r
 }
 
-func (hr *HTTPResponse) WithData(data interface{}) *HTTPResponse {
-	hr.Data = data
-	return hr
+func (r *HTTPResponse) WithData(data interface{}) *HTTPResponse {
+	if r == nil {
+		r = &HTTPResponse{}
+	}
+
+	r.Data = data
+
+	return r
 }
 
-func (hr *HTTPResponse) WithMeta(meta map[string]interface{}) *HTTPResponse {
-	hr.Meta = meta
-	return hr
+func (r *HTTPResponse) WithMeta(meta map[string]interface{}) *HTTPResponse {
+	if r == nil {
+		r = &HTTPResponse{}
+	}
+
+	r.Meta = meta
+
+	return r
 }
 
-func (hr *HTTPResponse) WithMessage(message string) *HTTPResponse {
-	hr.Message = message
-	return hr
+func (r *HTTPResponse) WithMessage(message string) *HTTPResponse {
+	if r == nil {
+		r = &HTTPResponse{}
+	}
+
+	r.Message = message
+
+	return r
 }
 
-func (hr *HTTPResponse) WithWarning(warning string) *HTTPResponse {
-	hr.Message = warning
-	return hr
+func (r *HTTPResponse) WithWarning(warning string) *HTTPResponse {
+	if r == nil {
+		r = &HTTPResponse{}
+	}
+
+	r.Warning = warning
+
+	return r
 }

+ 88 - 1
core/pkg/protocol/http_test.go

@@ -6,6 +6,7 @@ import (
 	"net/http"
 	"net/http/httptest"
 	"testing"
+	"time"
 
 	"github.com/stretchr/testify/assert"
 )
@@ -184,4 +185,90 @@ func TestHTTPProtocol_WriteData_Structure(t *testing.T) {
 	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")
+	}
+}