Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion testing/codegen/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func testingPath(_ string, svc *expr.ServiceExpr) string {
// hasStreams checks if the service has streaming methods.
func hasStreams(svc *expr.ServiceExpr) bool {
for _, m := range svc.Methods {
if m.Stream != expr.NoStreamKind {
if m.IsStreaming() {
return true
}
}
Expand Down
57 changes: 57 additions & 0 deletions testing/codegen/harness_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package codegen

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/codegen/service"
httpcodegen "goa.design/goa/v3/http/codegen"
"goa.design/plugins/v3/testing/codegen/testdata"
)

func TestGenerateHarness(t *testing.T) {
cases := map[string]struct {
DSL func()
Code map[string][]string
Path string
}{
"with-stream": {
DSL: testdata.WithStreamDSL,
Code: map[string][]string{
"http-harness": {testdata.WithStreamCode},
},
Path: "gen/with_stream_service/with_stream_servicetest/harness.go",
},
"without-stream": {
DSL: testdata.WithoutStreamDSL,
Code: map[string][]string{
"http-harness": {testdata.WithoutStreamCode},
},
Path: "gen/without_stream_service/without_stream_servicetest/harness.go",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
root := httpcodegen.RunHTTPDSL(t, c.DSL)
services := service.NewServicesData(root)
svc := root.Services[0]
svcData := services.Get(svc.Name)
f := generateHarness("", svcData, root, svc)
assert.Equal(t, c.Path, f.Path)
for sec, secCode := range c.Code {
testCode(t, f, sec, secCode)
}
})
}
}

func testCode(t *testing.T, file *codegen.File, section string, expCode []string) {
sections := file.Section(section)
require.Len(t, sections, len(expCode))
for i, c := range expCode {
code := codegen.SectionCode(t, sections[i])
assert.Equal(t, c, code)
}
}
80 changes: 43 additions & 37 deletions testing/codegen/templates/http_harness.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ func (h *Harness) setupHTTP() {

// Create HTTP handler
mux := goahttp.NewMuxer()
{{- if .HasStreams }}
// Create WebSocket upgrader for streaming endpoints
upgrader := &websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
server := httpsvr.New(endpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil, nil, upgrader, nil)
{{- end }}
server := httpsvr.New(endpoints, mux, goahttp.RequestDecoder, goahttp.ResponseEncoder, nil, nil{{ if .HasStreams }}, upgrader, nil{{ end }})
httpsvr.Mount(mux, server)

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

{{ printf "getHTTPClientImpl returns the underlying HTTP client implementation." | comment }}
func (h *Harness) getHTTPClientImpl() *httpcli.Client {
if h.httpSvr == nil || h.httpCli == nil {
h.t.Fatal("HTTP transport not configured")
}
u, err := url.Parse(h.httpSvr.URL)
if err != nil {
h.t.Fatalf("invalid test server URL: %v", err)
}
scheme := u.Scheme
host := u.Host
// Create WebSocket dialer for streaming endpoints
wsDialer := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
}

return httpcli.NewClient(
scheme,
host,
h.httpCli,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
false,
wsDialer,
nil,
)
if h.httpSvr == nil || h.httpCli == nil {
h.t.Fatal("HTTP transport not configured")
}
u, err := url.Parse(h.httpSvr.URL)
if err != nil {
h.t.Fatalf("invalid test server URL: %v", err)
}
scheme := u.Scheme
host := u.Host
{{- if .HasStreams }}
// Create WebSocket dialer for streaming endpoints
wsDialer := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
}
{{- end }}

return httpcli.NewClient(
scheme,
host,
h.httpCli,
goahttp.RequestEncoder,
goahttp.ResponseDecoder,
false,
{{- if .HasStreams }}
wsDialer,
nil,
{{- end }}
)
}

{{ printf "HTTPClientEndpoints creates HTTP client endpoints for the service." | comment }}
func (h *Harness) HTTPClientEndpoints() *{{ .PkgName }}.Endpoints {
c := h.getHTTPClientImpl()
return &{{ .PkgName }}.Endpoints{
{{- range .Methods }}
{{- $method := . }}
{{- range .Targets }}
{{- if or .IsHTTPPlain .IsHTTPServerSent .IsHTTPWebSocket }}
{{ $method.VarName }}: c.{{ $method.VarName }}(),
{{- break }}
{{- end }}
{{- end }}
{{- end }}
}
c := h.getHTTPClientImpl()
return &{{ .PkgName }}.Endpoints{
{{- range .Methods }}
{{- $method := . }}
{{- range .Targets }}
{{- if or .IsHTTPPlain .IsHTTPServerSent .IsHTTPWebSocket }}
{{ $method.VarName }}: c.{{ $method.VarName }}(),
{{- break }}
{{- end }}
{{- end }}
{{- end }}
}
}

{{ printf "HTTPURL returns the base URL of the test HTTP server." | comment }}
Expand Down
Loading
Loading