-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test method validation and api setup function
- Loading branch information
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package proxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package proxy | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type emptyThing struct{} | ||
|
||
func TestServer_setAPIConfig(t *testing.T) { | ||
specificExposedThing := &exposedThingA{} | ||
tests := []struct { | ||
name string | ||
s *Server | ||
api interface{} | ||
server interface{} | ||
result *Server | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "no functions", | ||
s: &Server{}, | ||
api: &emptyThing{}, | ||
server: &emptyThing{}, | ||
result: &Server{ | ||
api: []reflect.Method{}, | ||
innerServer: &emptyThing{}, | ||
}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "matching", | ||
s: &Server{}, | ||
api: specificExposedThing, | ||
server: &thingA{}, | ||
result: &Server{ | ||
api: []reflect.Method{ | ||
reflect.TypeOf(specificExposedThing).Method(0), | ||
}, | ||
innerServer: &thingA{}, | ||
}, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "mismatch", | ||
s: &Server{}, | ||
api: specificExposedThing, | ||
server: &thingB{}, | ||
result: &Server{}, | ||
expectedErr: "httpgrpc: validation of PostDoThing to DoThing mapping: api and server arguments mismatch: int vs string", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.s.setAPIConfig(tt.api, tt.server) | ||
if tt.expectedErr == "" { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.result.grpcServer, tt.s.grpcServer) | ||
assert.Equal(t, tt.result.innerServer, tt.s.innerServer) | ||
if assert.Len(t, tt.s.api, len(tt.result.api)) { | ||
for i, method := range tt.s.api { | ||
expectedMethod := tt.result.api[i] | ||
assert.Equal(t, method.Name, expectedMethod.Name) | ||
assert.Equal(t, method.PkgPath, expectedMethod.PkgPath) | ||
assert.Equal(t, method.Type, expectedMethod.Type) | ||
assert.Equal(t, method.Index, expectedMethod.Index) | ||
} | ||
} | ||
} else { | ||
assert.EqualError(t, err, tt.expectedErr) | ||
} | ||
}) | ||
} | ||
} |