Skip to content

Commit 1a48710

Browse files
authored
Support methods without streams by testing/codegen (#205)
* Add test for generateHarness() in testing/codegen * Support methods without streams by testing/codegen * Fix to tabbed indentation * Use IsStreaming() instead of NoStreamKind * Generate HTTPWSURL() only for methods with streams
1 parent 0313b42 commit 1a48710

5 files changed

Lines changed: 366 additions & 38 deletions

File tree

testing/codegen/harness.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func testingPath(_ string, svc *expr.ServiceExpr) string {
206206
// hasStreams checks if the service has streaming methods.
207207
func hasStreams(svc *expr.ServiceExpr) bool {
208208
for _, m := range svc.Methods {
209-
if m.Stream != expr.NoStreamKind {
209+
if m.IsStreaming() {
210210
return true
211211
}
212212
}

testing/codegen/harness_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"goa.design/goa/v3/codegen"
9+
"goa.design/goa/v3/codegen/service"
10+
httpcodegen "goa.design/goa/v3/http/codegen"
11+
"goa.design/plugins/v3/testing/codegen/testdata"
12+
)
13+
14+
func TestGenerateHarness(t *testing.T) {
15+
cases := map[string]struct {
16+
DSL func()
17+
Code map[string][]string
18+
Path string
19+
}{
20+
"with-stream": {
21+
DSL: testdata.WithStreamDSL,
22+
Code: map[string][]string{
23+
"http-harness": {testdata.WithStreamCode},
24+
},
25+
Path: "gen/with_stream_service/with_stream_servicetest/harness.go",
26+
},
27+
"without-stream": {
28+
DSL: testdata.WithoutStreamDSL,
29+
Code: map[string][]string{
30+
"http-harness": {testdata.WithoutStreamCode},
31+
},
32+
Path: "gen/without_stream_service/without_stream_servicetest/harness.go",
33+
},
34+
}
35+
for name, c := range cases {
36+
t.Run(name, func(t *testing.T) {
37+
root := httpcodegen.RunHTTPDSL(t, c.DSL)
38+
services := service.NewServicesData(root)
39+
svc := root.Services[0]
40+
svcData := services.Get(svc.Name)
41+
f := generateHarness("", svcData, root, svc)
42+
assert.Equal(t, c.Path, f.Path)
43+
for sec, secCode := range c.Code {
44+
testCode(t, f, sec, secCode)
45+
}
46+
})
47+
}
48+
}
49+
50+
func testCode(t *testing.T, file *codegen.File, section string, expCode []string) {
51+
sections := file.Section(section)
52+
require.Len(t, sections, len(expCode))
53+
for i, c := range expCode {
54+
code := codegen.SectionCode(t, sections[i])
55+
assert.Equal(t, c, code)
56+
}
57+
}

testing/codegen/templates/http_harness.go.tpl

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ func (h *Harness) setupHTTP() {
55

66
// Create HTTP handler
77
mux := goahttp.NewMuxer()
8+
{{- if .HasStreams }}
89
// Create WebSocket upgrader for streaming endpoints
910
upgrader := &websocket.Upgrader{
1011
CheckOrigin: func(r *http.Request) bool { return true },
1112
}
12-
server := httpsvr.New(endpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil, nil, upgrader, nil)
13+
{{- end }}
14+
server := httpsvr.New(endpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil, nil{{ if .HasStreams }}, upgrader, nil{{ end }})
1315
httpsvr.Mount(mux, server)
1416

1517
// Create test server
@@ -31,46 +33,50 @@ func (h *Harness) HTTPClient() *http.Client {
3133

3234
{{ printf "getHTTPClientImpl returns the underlying HTTP client implementation." | comment }}
3335
func (h *Harness) getHTTPClientImpl() *httpcli.Client {
34-
if h.httpSvr == nil || h.httpCli == nil {
35-
h.t.Fatal("HTTP transport not configured")
36-
}
37-
u, err := url.Parse(h.httpSvr.URL)
38-
if err != nil {
39-
h.t.Fatalf("invalid test server URL: %v", err)
40-
}
41-
scheme := u.Scheme
42-
host := u.Host
43-
// Create WebSocket dialer for streaming endpoints
44-
wsDialer := &websocket.Dialer{
45-
Proxy: http.ProxyFromEnvironment,
46-
}
47-
48-
return httpcli.NewClient(
49-
scheme,
50-
host,
51-
h.httpCli,
52-
goahttp.RequestEncoder,
53-
goahttp.ResponseDecoder,
54-
false,
55-
wsDialer,
56-
nil,
57-
)
36+
if h.httpSvr == nil || h.httpCli == nil {
37+
h.t.Fatal("HTTP transport not configured")
38+
}
39+
u, err := url.Parse(h.httpSvr.URL)
40+
if err != nil {
41+
h.t.Fatalf("invalid test server URL: %v", err)
42+
}
43+
scheme := u.Scheme
44+
host := u.Host
45+
{{- if .HasStreams }}
46+
// Create WebSocket dialer for streaming endpoints
47+
wsDialer := &websocket.Dialer{
48+
Proxy: http.ProxyFromEnvironment,
49+
}
50+
{{- end }}
51+
52+
return httpcli.NewClient(
53+
scheme,
54+
host,
55+
h.httpCli,
56+
goahttp.RequestEncoder,
57+
goahttp.ResponseDecoder,
58+
false,
59+
{{- if .HasStreams }}
60+
wsDialer,
61+
nil,
62+
{{- end }}
63+
)
5864
}
5965

6066
{{ printf "HTTPClientEndpoints creates HTTP client endpoints for the service." | comment }}
6167
func (h *Harness) HTTPClientEndpoints() *{{ .PkgName }}.Endpoints {
62-
c := h.getHTTPClientImpl()
63-
return &{{ .PkgName }}.Endpoints{
64-
{{- range .Methods }}
65-
{{- $method := . }}
66-
{{- range .Targets }}
67-
{{- if or .IsHTTPPlain .IsHTTPServerSent .IsHTTPWebSocket }}
68-
{{ $method.VarName }}: c.{{ $method.VarName }}(),
69-
{{- break }}
70-
{{- end }}
71-
{{- end }}
72-
{{- end }}
73-
}
68+
c := h.getHTTPClientImpl()
69+
return &{{ .PkgName }}.Endpoints{
70+
{{- range .Methods }}
71+
{{- $method := . }}
72+
{{- range .Targets }}
73+
{{- if or .IsHTTPPlain .IsHTTPServerSent .IsHTTPWebSocket }}
74+
{{ $method.VarName }}: c.{{ $method.VarName }}(),
75+
{{- break }}
76+
{{- end }}
77+
{{- end }}
78+
{{- end }}
79+
}
7480
}
7581

7682
{{ printf "HTTPURL returns the base URL of the test HTTP server." | comment }}
@@ -108,6 +114,7 @@ func (h *Harness) HTTPRequest(method, path string, body any) *http.Request {
108114
return req
109115
}
110116

117+
{{- if .HasStreams }}
111118
{{ printf "HTTPWSURL builds a websocket URL from the base HTTP URL and path." | comment }}
112119
func (h *Harness) HTTPWSURL(path string) string {
113120
base := h.HTTPURL()
@@ -123,6 +130,7 @@ func (h *Harness) HTTPWSURL(path string) string {
123130
u.Path = path
124131
return u.String()
125132
}
133+
{{- end }}
126134

127135
{{ printf "HTTPDo performs an HTTP request and returns the response." | comment }}
128136
func (h *Harness) HTTPDo(req *http.Request) *http.Response {

0 commit comments

Comments
 (0)