-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
60 lines (45 loc) · 1.57 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package negotiator_test
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/go-http-utils/negotiator"
)
func ExampleNegotiator_Accept() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept", "text/html, application/*;q=0.9, image/jpeg;q=0.8")
negotiator := negotiator.New(req.Header)
fmt.Println(negotiator.Type("text/html", "application/json", "image/jpeg"))
// -> "text/html"
fmt.Println(negotiator.Type("application/json", "image/jpeg", "text/plain"))
// -> "application/json"
fmt.Println(negotiator.Type("text/plain"))
// -> ""
}
func ExampleNegotiator_Encoding() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "gzip, compress;q=0.2, identity;q=0.5")
negotiator := negotiator.New(req.Header)
fmt.Println(negotiator.Encoding("identity", "gzip"))
// -> "gzip"
fmt.Println(negotiator.Encoding("compress", "identity"))
// -> "identity"
}
func ExampleNegotiator_Language() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Language", "en;q=0.8, es, pt")
negotiator := negotiator.New(req.Header)
fmt.Println(negotiator.Language("en", "es", "fr"))
// -> "es"
fmt.Println(negotiator.Language("es", "pt"))
// -> "es"
}
func ExampleNegotiator_Charset() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Accept-Language", "utf-8, iso-8859-1;q=0.8, utf-7;q=0.2")
negotiator := negotiator.New(req.Header)
fmt.Println(negotiator.Charset("UTF-8", "ISO-8859-1", "ISO-8859-5"))
// -> "UTF-8"
fmt.Println(negotiator.Charset("ISO-8859-5"))
// -> ""
}