Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding a check for null http handler before starting the server #138

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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 {
kappratiksha marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("no matching function found with name: %q", target)
}

return http.ListenAndServe(":"+port, handler)
kappratiksha marked this conversation as resolved.
Show resolved Hide resolved
}
kappratiksha marked this conversation as resolved.
Show resolved Hide resolved

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
}