Skip to content

Commit 1dbdd58

Browse files
authored
HTTP routes must include a prefix (#75)
1 parent b42cc16 commit 1dbdd58

8 files changed

Lines changed: 94 additions & 78 deletions

File tree

offline/handler_server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"sync"
1414
"time"
1515

16-
"github.com/terrable-dev/terrable/utils"
17-
1816
"github.com/fatih/color"
1917
"github.com/google/uuid"
2018
"github.com/gorilla/mux"
@@ -40,7 +38,7 @@ func ServeHandler(handlerInstance *HandlerInstance, r *mux.Router) {
4038
defer np.Close()
4139

4240
for method, path := range handlerInstance.handlerConfig.Http {
43-
go r.HandleFunc(utils.NormalisePath(path), func(w http.ResponseWriter, r *http.Request) {
41+
go r.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
4442
handlerExecutionMutex.Lock()
4543
defer handlerExecutionMutex.Unlock()
4644

offline/offline.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package offline
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"net"
78
"net/http"
89
"os"
10+
"strings"
911
"sync"
1012

1113
"github.com/fatih/color"
@@ -22,10 +24,10 @@ func Run(filePath string, moduleName string, port string) error {
2224
log.Fatalf("error running offline: %s", err)
2325
}
2426

25-
// TODO: Validate config
27+
err = validateConfig(terrableConfig)
2628

2729
if err != nil {
28-
panic(fmt.Errorf("error parsing .terrable.toml file: %w", err))
30+
return fmt.Errorf(`error validating configuration: %s`, err.Error())
2931
}
3032

3133
listener, activePort, err := getListener(port)
@@ -81,6 +83,24 @@ func Run(filePath string, moduleName string, port string) error {
8183
return nil
8284
}
8385

86+
func validateConfig(config *config.TerrableConfig) error {
87+
var errs []string
88+
89+
for _, handler := range config.Handlers {
90+
for method, path := range handler.Http {
91+
if !strings.HasPrefix(path, "/") {
92+
errs = append(errs, fmt.Sprintf("Handler '%s' does not have a '/' prefix for the HTTP route %s '%s'.", handler.Name, method, path))
93+
}
94+
}
95+
}
96+
97+
if len(errs) > 0 {
98+
return errors.New(strings.Join(errs, "\n"))
99+
}
100+
101+
return nil
102+
}
103+
84104
func getListener(port string) (net.Listener, int, error) {
85105
var specificPortDesired bool = (port != "")
86106

@@ -133,7 +153,7 @@ func printConfig(config config.TerrableConfig, port int) {
133153

134154
url := fmt.Sprintf("%s%s",
135155
hostColor(fmt.Sprintf("http://localhost:%d", port)),
136-
pathColor(utils.NormalisePath(path)))
156+
pathColor(path))
137157

138158
t.AppendRow(table.Row{
139159
methodColor(method),

offline/offline_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestPrintConfig(t *testing.T) {
2525
Name: "Handler2",
2626
Source: "source2",
2727
Http: map[string]string{
28-
"GET": "path2",
28+
"GET": "/path2",
2929
},
3030
},
3131
{
@@ -71,3 +71,67 @@ func TestPrintConfig(t *testing.T) {
7171
}
7272
}
7373
}
74+
75+
func TestValidateConfig(t *testing.T) {
76+
tests := []struct {
77+
name string
78+
config *config.TerrableConfig
79+
expectErr bool
80+
}{
81+
{
82+
name: "ValidConfig",
83+
config: &config.TerrableConfig{
84+
Handlers: []config.HandlerMapping{
85+
{
86+
Name: "Handler1",
87+
Source: "source1",
88+
Http: map[string]string{
89+
"GET": "/path1",
90+
"POST": "/path1",
91+
},
92+
},
93+
{
94+
Name: "Handler2",
95+
Source: "source2",
96+
Http: map[string]string{
97+
"GET": "/path2",
98+
},
99+
},
100+
},
101+
},
102+
expectErr: false,
103+
},
104+
{
105+
name: "InvalidConfig",
106+
config: &config.TerrableConfig{
107+
Handlers: []config.HandlerMapping{
108+
{
109+
Name: "Handler1",
110+
Source: "source1",
111+
Http: map[string]string{
112+
"GET": "path1",
113+
"POST": "/path1",
114+
},
115+
},
116+
{
117+
Name: "Handler2",
118+
Source: "source2",
119+
Http: map[string]string{
120+
"GET": "path2",
121+
},
122+
},
123+
},
124+
},
125+
expectErr: true,
126+
},
127+
}
128+
129+
for _, tt := range tests {
130+
t.Run(tt.name, func(t *testing.T) {
131+
err := validateConfig(tt.config)
132+
if (err != nil) != tt.expectErr {
133+
t.Errorf("validateConfig() error = %v, expectErr %v", err, tt.expectErr)
134+
}
135+
})
136+
}
137+
}

samples/simple/simple-api.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,21 @@ module "simple_api" {
4242
source = "./src/EchoCallback.ts"
4343
http = {
4444
GET = "/echo-callback"
45-
PUT = "/echo-callback"
4645
}
4746
},
4847

4948
# Echo Handler with no local environment variables
5049
EchoHandlerNoLocalEnv: {
5150
source = "./src/Echo.ts"
5251
http = {
53-
GET = "/echo-no-env",
52+
GET = "/echo-no-env"
5453
}
5554
},
5655

5756
DelayedHandler: {
5857
source = "./src/Delayed.ts"
5958
http = {
60-
GET = "/delayed",
59+
GET = "/delayed"
6160
}
6261
},
6362

@@ -74,13 +73,13 @@ module "simple_api" {
7473
CollisionOne: {
7574
source = "./src/Collision1/Collision.ts"
7675
http = {
77-
GET = "/collision1",
76+
GET = "/collision1"
7877
}
7978
},
8079
CollisionTwo: {
8180
source = "./src/Collision2/Collision.ts"
8281
http = {
83-
GET = "/collision2",
82+
GET = "/collision2"
8483
}
8584
}
8685
}

terrable_build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = 0.6.2
1+
version = 0.6.3

tests/requests/route_normalisation.hurl

Lines changed: 0 additions & 8 deletions
This file was deleted.

utils/normalise_path.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

utils/normalise_path_test.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)