Skip to content

Commit

Permalink
feat(issue-34): add validator for min http proto
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 committed Dec 18, 2023
1 parent a0da5d4 commit 435b1d8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
15 changes: 14 additions & 1 deletion http/httpvalidate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

package httpvalidate

import "net/http"
import (
"net/http"
)

// Validator represents an http.Request validator.
type Validator interface {
Expand Down Expand Up @@ -95,3 +97,14 @@ func ExactParams(names ...string) Validator {
return true
})
}

// MinProto
func MinProto(major, minor int) Validator {
return ValidatorFunc(func(w http.ResponseWriter, r *http.Request) bool {
if r.ProtoAtLeast(major, minor) {
return true
}
w.WriteHeader(http.StatusHTTPVersionNotSupported)
return false
})
}
42 changes: 42 additions & 0 deletions http/httpvalidate/validate_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,45 @@ func ExampleExactParams() {
fmt.Println(string(b))
//Output: Hello, world!
}

func ExampleMinProto() {
h := Request(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello, world!")
}),
MinProto(1, 0),
)

ls, err := net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
return
}

s := &http.Server{
Handler: h,
}
defer s.Shutdown(context.Background())

go func() {
s.Serve(ls)
}()

// net/http defaults to HTTP/1.1 for HTTP urls
resp, err := http.Get(fmt.Sprintf("http://%s?hello=world", ls.Addr()))
if err != nil {
log.Fatal(err)
return
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return
}

fmt.Println(string(b))
//Output: Hello, world!
}
46 changes: 46 additions & 0 deletions http/httpvalidate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,49 @@ func TestExactParams(t *testing.T) {
})
})
}

func TestMinProto(t *testing.T) {
t.Run("will return a 505 status code", func(t *testing.T) {
t.Run("if a http/1.0 request is received", func(t *testing.T) {
h := Request(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
MinProto(2, 0),
)

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, "http://example.com", nil)
r.ProtoMajor = 1
r.ProtoMinor = 0

h.ServeHTTP(w, r)

responseCode := w.Result().StatusCode
if !assert.Equal(t, http.StatusHTTPVersionNotSupported, responseCode) {
return
}
})

t.Run("if a http/1.1 request is received", func(t *testing.T) {
h := Request(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
MinProto(2, 0),
)

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, "http://example.com", nil)
r.ProtoMajor = 1
r.ProtoMinor = 1

h.ServeHTTP(w, r)

responseCode := w.Result().StatusCode
if !assert.Equal(t, http.StatusHTTPVersionNotSupported, responseCode) {
return
}
})
})
}

0 comments on commit 435b1d8

Please sign in to comment.