Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/gofr/cmd/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions pkg/gofr/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

8 changes: 8 additions & 0 deletions pkg/gofr/datasource/pubsub/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion pkg/gofr/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -179,3 +188,4 @@ func (r *Request) bindBinary(raw any) error {

return nil
}

30 changes: 30 additions & 0 deletions pkg/gofr/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions pkg/gofr/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions pkg/gofr/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading