Skip to content

Commit

Permalink
fix: adding a check for null http handler before starting the server (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kappratiksha authored Jun 21, 2022
1 parent ac973b4 commit 5d5bf7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions funcframework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func Start(port string) error {
}
}

if handler == nil {
return fmt.Errorf("no matching function found with name: %q", target)
}

return http.ListenAndServe(":"+port, handler)
}

Expand Down
17 changes: 16 additions & 1 deletion funcframework/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestHTTPFunction(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h, err := wrapHTTPFunction("/", tc.fn)
defer func() { handler = nil }()
if err != nil {
t.Fatalf("registerHTTPFunction(): %v", err)
}
Expand Down Expand Up @@ -519,6 +520,7 @@ func TestCloudEventFunction(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
h, err := wrapCloudEventFunction(ctx, "/", tc.fn)
defer func() { handler = nil }()
if err != nil {
t.Fatalf("registerCloudEventFunction(): %v", err)
}
Expand Down Expand Up @@ -591,7 +593,7 @@ func TestDeclarativeFunctionHTTP(t *testing.T) {
}); err != nil {
t.Fatalf("registerHTTPFunction(): %v", err)
}

defer func() { handler = nil }()
// register functions
functions.HTTP(funcName, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
Expand Down Expand Up @@ -620,6 +622,8 @@ func TestDeclarativeFunctionCloudEvent(t *testing.T) {
// register functions
functions.CloudEvent(funcName, dummyCloudEvent)

//cleanup global var
defer func() { handler = nil }()
if _, ok := registry.Default().GetRegisteredFunction(funcName); !ok {
t.Fatalf("could not get registered function: %s", funcName)
}
Expand All @@ -632,6 +636,17 @@ func TestDeclarativeFunctionCloudEvent(t *testing.T) {
}
}

func TestFunctionsNotRegisteredError(t *testing.T) {
funcName := "HelloWorld"
os.Setenv("FUNCTION_TARGET", funcName)

wantErr := fmt.Sprintf("no matching function found with name: %q", funcName)

if err := Start("0"); err.Error() != wantErr {
t.Fatalf("Expected error: %s and received error: %s", wantErr, err.Error())
}
}

func dummyCloudEvent(ctx context.Context, e cloudevents.Event) error {
return nil
}

0 comments on commit 5d5bf7a

Please sign in to comment.