Skip to content

Commit 2d41573

Browse files
committed
chore: handle errors for endpoint methods
Signed-off-by: Manuel de la Peña <mdelapenya@gmail.com>
1 parent a3adeaf commit 2d41573

File tree

2 files changed

+73
-25
lines changed

2 files changed

+73
-25
lines changed

microcks.go

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,63 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
6868

6969
// HttpEndpoint allows retrieving the Http endpoint where Microcks can be accessed
7070
// (you'd have to append '/api' to access APIs)
71-
func (container *MicrocksContainer) HttpEndpoint(ctx context.Context) string {
72-
ip, _ := container.Host(ctx)
73-
port, _ := container.MappedPort(ctx, DefaultHttpPort)
74-
return fmt.Sprintf("http://%s:%s", ip, port.Port())
71+
func (container *MicrocksContainer) HttpEndpoint(ctx context.Context) (string, error) {
72+
ip, err := container.Host(ctx)
73+
if err != nil {
74+
return "", err
75+
}
76+
77+
port, err := container.MappedPort(ctx, DefaultHttpPort)
78+
if err != nil {
79+
return "", err
80+
}
81+
82+
return fmt.Sprintf("http://%s:%s", ip, port.Port()), nil
7583
}
7684

7785
// SoapMockEndpoint get the exposed mock endpoint for a SOAP Service.
78-
func (container *MicrocksContainer) SoapMockEndpoint(ctx context.Context, service string, version string) string {
79-
return fmt.Sprintf("%s/soap/%s/%s", container.HttpEndpoint(ctx), service, version)
86+
func (container *MicrocksContainer) SoapMockEndpoint(ctx context.Context, service string, version string) (string, error) {
87+
endpoint, err := container.HttpEndpoint(ctx)
88+
if err != nil {
89+
return "", err
90+
}
91+
92+
return fmt.Sprintf("%s/soap/%s/%s", endpoint, service, version), nil
8093
}
8194

8295
// RestMockEndpoints get the exposed mock endpoint for a REST Service.
83-
func (container *MicrocksContainer) RestMockEndpoint(ctx context.Context, service string, version string) string {
84-
return fmt.Sprintf("%s/rest/%s/%s", container.HttpEndpoint(ctx), service, version)
96+
func (container *MicrocksContainer) RestMockEndpoint(ctx context.Context, service string, version string) (string, error) {
97+
endpoint, err := container.HttpEndpoint(ctx)
98+
if err != nil {
99+
return "", err
100+
}
101+
102+
return fmt.Sprintf("%s/rest/%s/%s", endpoint, service, version), nil
85103
}
86104

87105
// GraphQLMockEndpoint get the exposed mock endpoints for a GraphQL Service.
88-
func (container *MicrocksContainer) GrapQLMockEndpoint(ctx context.Context, service string, version string) string {
89-
return fmt.Sprintf("%s/graphql/%s/%s", container.HttpEndpoint(ctx), service, version)
106+
func (container *MicrocksContainer) GrapQLMockEndpoint(ctx context.Context, service string, version string) (string, error) {
107+
endpoint, err := container.HttpEndpoint(ctx)
108+
if err != nil {
109+
return "", err
110+
}
111+
112+
return fmt.Sprintf("%s/graphql/%s/%s", endpoint, service, version), nil
90113
}
91114

92115
// GrpcMockEndpoint get the exposed mock endpoint for a GRPC Service.
93-
func (container *MicrocksContainer) GrpcMockEndpoint(ctx context.Context) string {
94-
ip, _ := container.Host(ctx)
95-
port, _ := container.MappedPort(ctx, DefaultGrpcPort)
96-
return fmt.Sprintf("grpc://%s:%s", ip, port.Port())
116+
func (container *MicrocksContainer) GrpcMockEndpoint(ctx context.Context) (string, error) {
117+
ip, err := container.Host(ctx)
118+
if err != nil {
119+
return "", err
120+
}
121+
122+
port, err := container.MappedPort(ctx, DefaultGrpcPort)
123+
if err != nil {
124+
return "", err
125+
}
126+
127+
return fmt.Sprintf("grpc://%s:%s", ip, port.Port()), nil
97128
}
98129

99130
// ImportAsMainArtifact imports an artifact as a primary or main one within the Microcks container.
@@ -109,7 +140,10 @@ func (container *MicrocksContainer) ImportAsSecondaryArtifact(ctx context.Contex
109140
// TestEndpoint launches a conformance test on an endpoint.
110141
func (container *MicrocksContainer) TestEndpoint(ctx context.Context, testRequest *client.TestRequest) (*client.TestResult, error) {
111142
// Retrieve API endpoint.
112-
httpEndpoint := container.HttpEndpoint(ctx)
143+
httpEndpoint, err := container.HttpEndpoint(ctx)
144+
if err != nil {
145+
return nil, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
146+
}
113147

114148
// Create Microcks client.
115149
c, err := client.NewClientWithResponses(httpEndpoint + "/api")
@@ -154,7 +188,10 @@ func (container *MicrocksContainer) TestEndpoint(ctx context.Context, testReques
154188

155189
func (container *MicrocksContainer) importArtifact(ctx context.Context, artifactFilePath string, mainArtifact bool) (int, error) {
156190
// Retrieve API endpoint.
157-
httpEndpoint := container.HttpEndpoint(ctx)
191+
httpEndpoint, err := container.HttpEndpoint(ctx)
192+
if err != nil {
193+
return http.StatusInternalServerError, fmt.Errorf("error retrieving Microcks API endpoint: %w", err)
194+
}
158195

159196
// Create Microcks client.
160197
c, err := client.NewClientWithResponses(httpEndpoint + "/api")

microcks_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,33 @@ func TestContractTestingFunctionality(t *testing.T) {
136136
}
137137

138138
func testConfigRetrieval(t *testing.T, ctx context.Context, microcksContainer *microcks.MicrocksContainer) {
139-
uri := microcksContainer.HttpEndpoint(ctx)
139+
uri, err := microcksContainer.HttpEndpoint(ctx)
140+
require.NoError(t, err)
141+
140142
resp, err := http.Get(uri + "/api/keycloak/config")
141143
require.NoError(t, err)
142144
require.Equal(t, http.StatusOK, resp.StatusCode)
143145
}
144146

145147
func testMockEndpoints(t *testing.T, ctx context.Context, microcksContainer *microcks.MicrocksContainer) {
146-
baseApiUrl := microcksContainer.SoapMockEndpoint(ctx, "Pastries Service", "1.0")
147-
require.Equal(t, microcksContainer.HttpEndpoint(ctx)+"/soap/Pastries Service/1.0", baseApiUrl)
148+
endpoint, err := microcksContainer.HttpEndpoint(ctx)
149+
require.NoError(t, err)
150+
151+
baseApiUrl, err := microcksContainer.SoapMockEndpoint(ctx, "Pastries Service", "1.0")
152+
require.NoError(t, err)
153+
require.Equal(t, endpoint+"/soap/Pastries Service/1.0", baseApiUrl)
148154

149-
baseApiUrl = microcksContainer.RestMockEndpoint(ctx, "API Pastries", "0.0.1")
150-
require.Equal(t, microcksContainer.HttpEndpoint(ctx)+"/rest/API Pastries/0.0.1", baseApiUrl)
155+
baseApiUrl, err = microcksContainer.RestMockEndpoint(ctx, "API Pastries", "0.0.1")
156+
require.NoError(t, err)
157+
require.Equal(t, endpoint+"/rest/API Pastries/0.0.1", baseApiUrl)
151158

152-
baseApiUrl = microcksContainer.GrapQLMockEndpoint(ctx, "Pastries Graph", "1")
153-
require.Equal(t, microcksContainer.HttpEndpoint(ctx)+"/graphql/Pastries Graph/1", baseApiUrl)
159+
baseApiUrl, err = microcksContainer.GrapQLMockEndpoint(ctx, "Pastries Graph", "1")
160+
require.NoError(t, err)
161+
require.Equal(t, endpoint+"/graphql/Pastries Graph/1", baseApiUrl)
162+
163+
baseGrpcUrl, err := microcksContainer.GrpcMockEndpoint(ctx)
164+
require.NoError(t, err)
154165

155-
baseGrpcUrl := microcksContainer.GrpcMockEndpoint(ctx)
156166
ip, err := microcksContainer.Host(ctx)
157167
require.NoError(t, err)
158168

@@ -162,7 +172,8 @@ func testMockEndpoints(t *testing.T, ctx context.Context, microcksContainer *mic
162172
}
163173

164174
func testMicrocksMockingFunctionality(t *testing.T, ctx context.Context, microcksContainer *microcks.MicrocksContainer) {
165-
baseApiUrl := microcksContainer.RestMockEndpoint(ctx, "API Pastries", "0.0.1")
175+
baseApiUrl, err := microcksContainer.RestMockEndpoint(ctx, "API Pastries", "0.0.1")
176+
require.NoError(t, err)
166177

167178
resp, err := http.Get(baseApiUrl + "/pastries/Millefeuille")
168179
require.NoError(t, err)

0 commit comments

Comments
 (0)