From 59f7404df75294dd71c076c89ee960407bbfe34d Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Tue, 13 Sep 2022 11:59:53 +0200
Subject: [PATCH] new ContentType function

---
 README.md   | 33 ++++++++++++++++++++++++++++++++-
 api.go      |  5 +++++
 api_test.go | 18 ++++++++++++++++++
 header.go   |  1 +
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 28761cd..647eaa1 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,28 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
 
 ```
 
+### Accept Content Type
+
+With the `ContentType` function you can negotiate the content type of HTTP request.
+
+```go
+package main
+
+import (
+	"net/http"
+	"gitlab.schukai.com/oss/libraries/go/network/http-negotiation"
+)
+
+func handleRequest(w http.ResponseWriter, r *http.Request) {
+	n := negotiation.New(r.Header)
+
+	if n.ContentType("application/json") != "" {
+		// ...
+	}
+
+}
+```
+
 ### Language Negotiation
 
 With the `Language` function you can negotiate the language of HTTP request.
@@ -66,7 +88,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
 	}
 
 }
-
 ```
 
 ### Charset Negotiation
@@ -115,6 +136,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
 
 ```
 
+## Changelog
+
+### 1.1.0
+
+* Add `ContentType` function
+
+### 1.0.0   
+
+* Initial release
+
 ## Contributing
 
 Merge requests are welcome. For major changes, please open an issue first to discuss what 
diff --git a/api.go b/api.go
index 8ae6596..fa394b2 100644
--- a/api.go
+++ b/api.go
@@ -14,6 +14,11 @@ func New(header http.Header) *Negotiation {
 	return &Negotiation{header}
 }
 
+func (n *Negotiation) ContentType(acceptance ...string) string {
+	parser := newHeaderParser(n.header, false)
+	return parser.selectOffer(acceptance, parser.parse(headerContentType))
+}
+
 // Type returns the most preferred content type from the HTTP Accept header.
 // If nothing accepted, then empty string is returned.
 func (n *Negotiation) Type(offers ...string) (bestOffer string) {
diff --git a/api_test.go b/api_test.go
index a976912..c59933e 100644
--- a/api_test.go
+++ b/api_test.go
@@ -5,6 +5,24 @@ import (
 	"testing"
 )
 
+func TestContentType(t *testing.T) {
+
+	req, err := http.NewRequest("GET", "/", nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	req.Header.Set("Content-Type", "application/json")
+
+	n := New(req.Header)
+
+	if n.ContentType("application/json") != "application/json" {
+		t.Errorf("handler returned wrong status code: got %v want %v",
+			n.Type("application/json"), "application/json")
+	}
+
+}
+
 func TestNegotiationLanguage(t *testing.T) {
 
 	req, err := http.NewRequest("GET", "/", nil)
diff --git a/header.go b/header.go
index c651276..69bb5b0 100644
--- a/header.go
+++ b/header.go
@@ -5,4 +5,5 @@ const (
 	headerAcceptLanguage = "Accept-Language"
 	headerAcceptEncoding = "Accept-Encoding"
 	headerAcceptCharset  = "Accept-Charset"
+	headerContentType    = "Content-Type"
 )
-- 
GitLab