diff --git a/pkg/gofr/cmd/request.go b/pkg/gofr/cmd/request.go index 444c2f71aa..fe12356425 100644 --- a/pkg/gofr/cmd/request.go +++ b/pkg/gofr/cmd/request.go @@ -86,6 +86,14 @@ func (*Request) HostName() (hostname string) { return hostname } +func (r *Request) Headers() map[string][]string { + return make(map[string][]string) // Not applicable for CMD, can be implemented if needed +} + +func (r *Request) Header(key string) string { + return "" // Not applicable for CMD, can be implemented if needed +} + // Params retrieves all values for a given query parameter key, including comma-separated values. func (r *Request) Params(key string) []string { value, exists := r.params[key] diff --git a/pkg/gofr/cron.go b/pkg/gofr/cron.go index 1e3f740d58..a393300c2a 100644 --- a/pkg/gofr/cron.go +++ b/pkg/gofr/cron.go @@ -183,6 +183,15 @@ func (noopRequest) Bind(any) error { return nil } +func (noopRequest) Headers() map[string][]string { + return nil +} + +func (noopRequest) Header(string) string { + return "" +} + func (noopRequest) Params(string) []string { return nil } + diff --git a/pkg/gofr/datasource/pubsub/message.go b/pkg/gofr/datasource/pubsub/message.go index adf5f78c09..93b8b6c8a0 100644 --- a/pkg/gofr/datasource/pubsub/message.go +++ b/pkg/gofr/datasource/pubsub/message.go @@ -110,6 +110,14 @@ func (*Message) HostName() string { return "" } +func (*Message) Headers() map[string][]string { + return make(map[string][]string) +} + +func (m *Message) Header(key string) string { + return "" +} + func (*Message) Params(string) []string { return nil } diff --git a/pkg/gofr/http/request.go b/pkg/gofr/http/request.go index 03eb667d65..e607a65831 100644 --- a/pkg/gofr/http/request.go +++ b/pkg/gofr/http/request.go @@ -88,6 +88,14 @@ func (r *Request) HostName() string { return fmt.Sprintf("%s://%s", proto, r.req.Host) } +func (r *Request) Headers() map[string][]string { + return r.req.Header +} + +func (r *Request) Header(key string) string { + return r.req.Header.Get(key) +} + // Params returns a slice of strings containing the values associated with the given query parameter key. // If the parameter is not present, an empty slice is returned. func (r *Request) Params(key string) []string { @@ -98,10 +106,11 @@ func (r *Request) Params(key string) []string { for _, value := range values { result = append(result, strings.Split(value, ",")...) } - + return result } + func (r *Request) body() ([]byte, error) { bodyBytes, err := io.ReadAll(r.req.Body) if err != nil { @@ -179,3 +188,4 @@ func (r *Request) bindBinary(raw any) error { return nil } + diff --git a/pkg/gofr/http/request_test.go b/pkg/gofr/http/request_test.go index 3e3c5e6326..350c9bf01f 100644 --- a/pkg/gofr/http/request_test.go +++ b/pkg/gofr/http/request_test.go @@ -317,3 +317,33 @@ func TestBind_BinaryOctetStream_NotPointerToByteSlice(t *testing.T) { t.Errorf("Expected error to contain: input is not a pointer to a byte slice: invalid input, got: %v", err) } } + +func TestHeaders(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/abc", http.NoBody) + req.Header.Add("X-Custom-Header", "value1") + req.Header.Add("X-Custom-Header", "value2") + req.Header.Add("Content-Type", "application/json") + + r := NewRequest(req) + headers := r.Headers() + + if len(headers) != 2 { + t.Errorf("Expected 2 headers, got %d", len(headers)) + } + + if headers["X-Custom-Header"][0] != "value1" || headers["X-Custom-Header"][1] != "value2" { + t.Errorf("X-Custom-Header values do not match expected values") + } +} + +func TestHeaders_Header(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/abc", http.NoBody) + req.Header.Add("X-Custom-Header", "value1") + req.Header.Add("Content-Type", "application/json") + + r := NewRequest(req) + header := r.Header("X-Custom-Header") + if header != "value1" { + t.Errorf("Expected X-Custom-Header to be 'value1', got '%s'", header) + } +} \ No newline at end of file diff --git a/pkg/gofr/request.go b/pkg/gofr/request.go index 3b836d4388..82f9cdd3df 100644 --- a/pkg/gofr/request.go +++ b/pkg/gofr/request.go @@ -13,5 +13,7 @@ type Request interface { PathParam(string) string Bind(any) error HostName() string + Headers() map[string][]string + Header(string) string Params(string) []string } diff --git a/pkg/gofr/websocket/websocket.go b/pkg/gofr/websocket/websocket.go index 52e5bbf6ac..e818b2803e 100644 --- a/pkg/gofr/websocket/websocket.go +++ b/pkg/gofr/websocket/websocket.go @@ -60,6 +60,14 @@ func (*Connection) Param(_ string) string { return "" // Not applicable for WebSocket, can be implemented if needed } +func (c *Connection) Headers() map[string][]string { + return make(map[string][]string) // Not applicable for WebSocket, can be implemented if needed +} + +func (w *Connection) Header(key string) string { + return "" // Not applicable for WebSocket, can be implemented if needed +} + func (*Connection) PathParam(_ string) string { return "" // Not applicable for WebSocket, can be implemented if needed }