Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.

Commit bf20aaf

Browse files
committed
fix critical bug related to affecting base url
1 parent 85c3f0e commit bf20aaf

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

pkg/service/internal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
func Test_extend(t *testing.T) {
1111
tests := []struct {
1212
name string
13-
url *url.URL
13+
url url.URL
1414
paths []string
1515
expected string
1616
}{
17-
{"without paths", &url.URL{Path: "/"}, nil, "/"},
18-
{"with some paths", &url.URL{Path: "/with"}, []string{"some", "paths"}, "/with/some/paths"},
17+
{"without paths", url.URL{Path: "/"}, nil, "/"},
18+
{"with some paths", url.URL{Path: "/with"}, []string{"some", "paths"}, "/with/some/paths"},
1919
}
2020

2121
for _, test := range tests {

pkg/service/service.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
repository "github.com/kamilsk/form-api/pkg/storage/types"
15+
1416
"github.com/kamilsk/form-api/pkg/config"
1517
"github.com/kamilsk/form-api/pkg/domain"
1618
"github.com/kamilsk/form-api/pkg/errors"
@@ -87,14 +89,30 @@ func (service *Forma) HandleInput(ctx context.Context, req v1.PostRequest) (resp
8789
return
8890
}
8991

90-
input, storeErr := service.storage.StoreInput(ctx, req.ID, verified)
92+
var input *repository.Input
93+
input, resp.Error = service.storage.StoreInput(ctx, req.ID, verified)
94+
95+
if resp.Error == nil && !req.Context.Option().NoLog && !ignore(req.Context) {
96+
// if option.Anonymously {}
97+
event := domain.InputEvent{
98+
SchemaID: req.ID,
99+
InputID: input.ID,
100+
TemplateID: req.InputData.Template(),
101+
Identifier: nil, // TODO issue#171
102+
Context: req.Context,
103+
Code: http.StatusFound, // TODO issue#design
104+
URL: resp.URL,
105+
}
106+
resp.Error = service.tracker.LogInput(ctx, event) // TODO issue#109
107+
}
108+
91109
if u, err := url.Parse(resp.URL); err == nil {
92110
type feedback struct {
93111
ID domain.ID `json:"input"`
94112
SchemaID domain.ID `json:"id"`
95113
Result string `json:"result"`
96114
}
97-
if storeErr != nil {
115+
if resp.Error != nil {
98116
u.Fragment = base64.StdEncoding.EncodeToString(func() []byte {
99117
raw, _ := json.Marshal(feedback{SchemaID: req.ID, Result: "failure"})
100118
return raw
@@ -108,20 +126,6 @@ func (service *Forma) HandleInput(ctx context.Context, req v1.PostRequest) (resp
108126
resp.URL = u.String()
109127
}
110128

111-
if storeErr == nil && !req.Context.Option().NoLog && !ignore(req.Context) {
112-
// if option.Anonymously {}
113-
event := domain.InputEvent{
114-
SchemaID: req.ID,
115-
InputID: input.ID,
116-
TemplateID: req.InputData.Template(),
117-
Identifier: nil, // TODO issue#171
118-
Context: req.Context,
119-
Code: http.StatusFound, // TODO issue#design
120-
URL: resp.URL,
121-
}
122-
resp.Error = service.tracker.LogInput(ctx, event) // TODO issue#109
123-
}
124-
125129
return resp
126130
}
127131

@@ -130,12 +134,12 @@ func enrich(base *url.URL, schema *domain.Schema) {
130134
schema.Inputs[i].ID = schema.ID + "_" + schema.Inputs[i].Name
131135
}
132136
// replace fallback by current API call
133-
schema.Action = extend(base, "api/v1", schema.ID)
137+
schema.Action = extend(*base, "api/v1", schema.ID)
134138
schema.Method = http.MethodPost
135139
schema.EncodingType = "application/x-www-form-urlencoded"
136140
}
137141

138-
func extend(base *url.URL, paths ...string) string {
142+
func extend(base url.URL, paths ...string) string {
139143
if len(paths) == 0 {
140144
return base.String()
141145
}

0 commit comments

Comments
 (0)