Skip to content

Commit

Permalink
Fix testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
emcfarlane committed Aug 6, 2024
1 parent 74290d8 commit cbeccdd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ProcedureFromURL(url *url.URL) (string, bool) {
if ultimate == len(path)-1 || penultimate == ultimate-1 {
return url.Path, false
}
return procedure, false
return procedure, true
}

// A Protocol defines the HTTP semantics to use when sending and receiving
Expand Down
77 changes: 76 additions & 1 deletion protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package connect

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

Expand Down Expand Up @@ -65,6 +67,78 @@ func BenchmarkCanonicalizeContentType(b *testing.B) {
})
}

func TestProtocolFromRequest(t *testing.T) {
t.Parallel()
tests := []struct {
name string
contentType string
method string
want string
valid bool
}{{
name: "connectUnary",
contentType: "application/json",
method: http.MethodPost,
want: ProtocolConnect,
valid: true,
}, {
name: "connectStreaming",
contentType: "application/connec+json",
method: http.MethodPost,
want: ProtocolConnect,
valid: true,
}, {
name: "grpcWeb",
contentType: "application/grpc-web",
method: http.MethodPost,
want: ProtocolGRPCWeb,
valid: true,
}, {
name: "grpc",
contentType: "application/grpc",
method: http.MethodPost,
want: ProtocolGRPC,
valid: true,
}, {
name: "connectGet",
contentType: "application/connec+json",
method: http.MethodGet,
want: ProtocolConnect,
valid: true,
}, {
name: "grpcWebGet",
contentType: "application/grpc-web",
method: http.MethodGet,
want: ProtocolConnect,
valid: true,
}, {
name: "grpcGet",
contentType: "application/grpc+json",
method: http.MethodGet,
want: ProtocolConnect,
valid: true,
}, {
name: "unknown",
contentType: "text/html",
method: http.MethodPost,
valid: false,
}}
for _, testcase := range tests {
testcase := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(testcase.method, "http://localhost:8080/service/Method", nil)
if testcase.contentType != "" {
req.Header.Set("Content-Type", testcase.contentType)
}
req.Method = testcase.method
got, valid := ProtocolFromRequest(req)
assert.Equal(t, got, testcase.want, assert.Sprintf("protocol"))
assert.Equal(t, valid, testcase.valid, assert.Sprintf("valid"))
})
}
}

func TestProcedureFromURL(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down Expand Up @@ -96,8 +170,9 @@ func TestProcedureFromURL(t *testing.T) {
return
}
t.Log(url.String())
got, _ := ProcedureFromURL(url)
got, valid := ProcedureFromURL(url)
assert.Equal(t, got, testcase.want)
assert.Equal(t, valid, testcase.valid)
})
}
}

0 comments on commit cbeccdd

Please sign in to comment.