1 package httpencoding_test 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 9 "vimagination.zapto.org/httpencoding" 10 ) 11 12 func Example() { 13 handler := func(w http.ResponseWriter, r *http.Request) { 14 if !httpencoding.HandleEncoding(r, httpencoding.HandlerFunc(func(e httpencoding.Encoding) bool { 15 if e == "gzip" || httpencoding.IsWildcard(e) && !httpencoding.IsDisallowedInWildcard(e, "gzip") { 16 io.WriteString(w, "gzip") 17 } else if e == "" || httpencoding.IsWildcard(e) && !httpencoding.IsDisallowedInWildcard(e, "") { 18 io.WriteString(w, "identity") 19 } else { 20 return false 21 } 22 23 return true 24 })) { 25 io.WriteString(w, "none") 26 } 27 } 28 29 w := httptest.NewRecorder() 30 r, _ := http.NewRequest(http.MethodGet, "/", nil) 31 handler(w, r) 32 fmt.Println(w.Body) 33 34 w = httptest.NewRecorder() 35 r.Header.Set("Accept-encoding", "identity") 36 handler(w, r) 37 fmt.Println(w.Body) 38 39 w = httptest.NewRecorder() 40 r.Header.Set("Accept-encoding", "gzip, identity") 41 handler(w, r) 42 fmt.Println(w.Body) 43 44 w = httptest.NewRecorder() 45 r.Header.Set("Accept-encoding", "gzip;q=0.5, identity;q=0.6") 46 handler(w, r) 47 fmt.Println(w.Body) 48 49 w = httptest.NewRecorder() 50 r.Header.Set("Accept-encoding", "identity;q=0") 51 handler(w, r) 52 fmt.Println(w.Body) 53 54 // Output: 55 // gzip 56 // identity 57 // gzip 58 // identity 59 // none 60 } 61