Skip to content

Commit

Permalink
feat: Simple extraction of Content-Type header is now possible throug…
Browse files Browse the repository at this point in the history
…h helper method
  • Loading branch information
randuck-dev committed Nov 6, 2023
1 parent 86d11aa commit 716b97a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
11 changes: 0 additions & 11 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ type Client interface {
Get(string) (Response, error)
}

type Response struct {
StatusLine StatusLine
Headers map[string]string
}

type StatusLine struct {
HttpVersion string
StatusCode uint16
ReasonPhrase string
}

type HttpClient struct {
net.Conn
}
Expand Down
27 changes: 27 additions & 0 deletions internal/http/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package http

import "errors"

type Response struct {
StatusLine StatusLine
Headers map[string]string
}

type StatusLine struct {
HttpVersion string
StatusCode uint16
ReasonPhrase string
}

var ErrNoContentTypefound = errors.New("no content type")

func (r Response) ContentType() (string, error) {

res, ok := r.Headers["Content-Type"]

if !ok {
return "", ErrNoContentTypefound
}

return res, nil
}
34 changes: 34 additions & 0 deletions internal/http/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package http

import "testing"

func TestContentType(t *testing.T) {

t.Run("application/json is set", func(t *testing.T) {
resp := Response{
Headers: map[string]string{
"Content-Type": "application/json",
},
}

content_type, err := resp.ContentType()

if err != nil {
t.Errorf("unexpected error occured: %s", err)
}

if content_type != "application/json" {
t.Errorf("got %s want %s", content_type, "application/json")
}
})

t.Run("header is not set", func(t *testing.T) {
resp := Response{}

_, err := resp.ContentType()

if err != ErrNoContentTypefound {
t.Errorf("got %s want %s", err, ErrNoContentTypefound)
}
})
}

0 comments on commit 716b97a

Please sign in to comment.