refactor: port - http - improve tests
ci/woodpecker/push/govulncheck Pipeline was successful Details
ci/woodpecker/push/test Pipeline was successful Details

This commit is contained in:
Dawid Wysokiński 2024-03-15 07:38:38 +01:00
parent 5b68c58797
commit 02108fcb00
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
8 changed files with 54 additions and 22 deletions

View File

@ -31,7 +31,7 @@ var (
apiServerHandlerTimeoutFlag = &cli.DurationFlag{
Name: "api.handlerTimeout",
EnvVars: []string{"API_HANDLER_TIMEOUT"},
Value: 5 * time.Second, //nolint:gomnd,
Value: 5 * time.Second, //nolint:gomnd
Usage: "https://pkg.go.dev/net/http#TimeoutHandler",
}
apiServerReadTimeoutFlag = &cli.DurationFlag{

View File

@ -585,7 +585,7 @@ func TestListEnnoblements(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListEnnoblements, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointListEnnoblements, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1232,7 +1232,12 @@ func TestListPlayerEnnoblements(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListPlayerEnnoblements, domaintest.RandVersionCode(), server.Key, player.Id)
req.URL.Path = fmt.Sprintf(
endpointListPlayerEnnoblements,
randInvalidVersionCode(t, handler),
server.Key,
player.Id,
)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1932,7 +1937,7 @@ func TestListTribeEnnoblements(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListTribeEnnoblements, domaintest.RandVersionCode(), server.Key, tribe.Id)
req.URL.Path = fmt.Sprintf(endpointListTribeEnnoblements, randInvalidVersionCode(t, handler), server.Key, tribe.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -2624,7 +2629,12 @@ func TestListVillageEnnoblements(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListVillageEnnoblements, domaintest.RandVersionCode(), server.Key, village.Id)
req.URL.Path = fmt.Sprintf(
endpointListVillageEnnoblements,
randInvalidVersionCode(t, handler),
server.Key,
village.Id,
)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()

View File

@ -710,7 +710,7 @@ func TestListPlayers(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListPlayers, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointListPlayers, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1228,7 +1228,7 @@ func TestListTribeMembers(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListTribeMembers, domaintest.RandVersionCode(), server.Key, tribeID)
req.URL.Path = fmt.Sprintf(endpointListTribeMembers, randInvalidVersionCode(t, handler), server.Key, tribeID)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1413,7 +1413,7 @@ func TestGetPlayer(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetPlayer, domaintest.RandVersionCode(), player.Server.Key, player.Id)
req.URL.Path = fmt.Sprintf(endpointGetPlayer, randInvalidVersionCode(t, handler), player.Server.Key, player.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()

View File

@ -386,7 +386,7 @@ func TestListServers(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListServers, domaintest.RandVersionCode())
req.URL.Path = fmt.Sprintf(endpointListServers, randInvalidVersionCode(t, handler))
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -500,7 +500,7 @@ func TestGetServer(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetServer, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointGetServer, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -603,7 +603,7 @@ func TestGetServerConfig(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetServerConfig, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointGetServerConfig, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -710,7 +710,7 @@ func TestGetServerUnitInfo(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointGetServerUnitInfo, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -817,7 +817,7 @@ func TestGetServerBuildingInfo(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointGetServerBuildingInfo, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()

View File

@ -600,7 +600,12 @@ func TestListPlayerTribeChanges(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListPlayerTribeChanges, domaintest.RandVersionCode(), server.Key, player.Id)
req.URL.Path = fmt.Sprintf(
endpointListPlayerTribeChanges,
randInvalidVersionCode(t, handler),
server.Key,
player.Id,
)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1326,7 +1331,7 @@ func TestListTribeTribeChanges(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListTribeTribeChanges, domaintest.RandVersionCode(), server.Key, tribe.Id)
req.URL.Path = fmt.Sprintf(endpointListTribeTribeChanges, randInvalidVersionCode(t, handler), server.Key, tribe.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()

View File

@ -636,7 +636,7 @@ func TestListTribes(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListTribes, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointListTribes, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -786,7 +786,7 @@ func TestGetTribe(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetTribe, domaintest.RandVersionCode(), tribe.Server.Key, tribe.Id)
req.URL.Path = fmt.Sprintf(endpointGetTribe, randInvalidVersionCode(t, handler), tribe.Server.Key, tribe.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()

View File

@ -384,7 +384,7 @@ func TestGetVersion(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetVersion, domaintest.RandVersionCode())
req.URL.Path = fmt.Sprintf(endpointGetVersion, randInvalidVersionCode(t, handler))
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -438,3 +438,20 @@ func getAllVersions(tb testing.TB, h http.Handler) []apimodel.Version {
require.NotEmpty(tb, bodyVersions.Data)
return bodyVersions.Data
}
func randInvalidVersionCode(tb testing.TB, h http.Handler) string {
tb.Helper()
versions := getAllVersions(tb, h)
for range 3 {
code := domaintest.RandVersionCode()
if !slices.ContainsFunc(versions, func(version apimodel.Version) bool {
return version.Code == code
}) {
return code
}
}
return domaintest.RandVersionCode()
}

View File

@ -410,7 +410,7 @@ func TestListVillages(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListVillages, domaintest.RandVersionCode(), server.Key)
req.URL.Path = fmt.Sprintf(endpointListVillages, randInvalidVersionCode(t, handler), server.Key)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -788,7 +788,7 @@ func TestListPlayerVillages(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListPlayerVillages, domaintest.RandVersionCode(), server.Key, player.Id)
req.URL.Path = fmt.Sprintf(endpointListPlayerVillages, randInvalidVersionCode(t, handler), server.Key, player.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1201,7 +1201,7 @@ func TestListTribeVillages(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointListTribeVillages, domaintest.RandVersionCode(), server.Key, tribe.Id)
req.URL.Path = fmt.Sprintf(endpointListTribeVillages, randInvalidVersionCode(t, handler), server.Key, tribe.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()
@ -1386,7 +1386,7 @@ func TestGetVillage(t *testing.T) {
name: "ERR: version not found",
reqModifier: func(t *testing.T, req *http.Request) {
t.Helper()
req.URL.Path = fmt.Sprintf(endpointGetVillage, domaintest.RandVersionCode(), village.Server.Key, village.Id)
req.URL.Path = fmt.Sprintf(endpointGetVillage, randInvalidVersionCode(t, handler), village.Server.Key, village.Id)
},
assertResp: func(t *testing.T, req *http.Request, resp *http.Response) {
t.Helper()