-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Simple extraction of Content-Type header is now possible throug…
…h helper method
- Loading branch information
1 parent
86d11aa
commit 716b97a
Showing
3 changed files
with
61 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |