diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..41f6461 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/Ocp +*.env* \ No newline at end of file diff --git a/README.md b/README.md index d0aee10..6c16014 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # GoWebAPI Example Go web API + +Hello diff --git a/go.mod b/go.mod index b48dafc..d8b2b88 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module main +module github.com/Sathwik004/GoWebAPI go 1.16 diff --git a/main.go b/main.go index 834cac5..27848f2 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { func whoAmI(response http.ResponseWriter, r *http.Request) { who := []whoami{ - whoami{Name: "Michael Levan", + {Name: "Intern at IBM", Title: "Kubernetes Engineer", State: "NJ", }, @@ -31,21 +31,27 @@ func whoAmI(response http.ResponseWriter, r *http.Request) { } func homePage(response http.ResponseWriter, r *http.Request) { - fmt.Fprintf(response, "Welcome to the Go Web API!") + fmt.Fprintf(response, "Welcome to the Go Web API!\n\nOther endpoints\n- /whoami\n- /aboutme\n- /ping") fmt.Println("Endpoint Hit: homePage") } func aboutMe(response http.ResponseWriter, r *http.Request) { - who := "MichaelLevan" + who := "Intern at IBM" - fmt.Fprintf(response, "A little bit about Michael Levan...") + fmt.Fprintf(response, "A little bit about Intern at IBM...") fmt.Println("Endpoint Hit: ", who) } +func ping(response http.ResponseWriter, r *http.Request) { + fmt.Fprintf(response, "pong") + fmt.Println("Endpoint Hit: ping") +} + func request1() { http.HandleFunc("/", homePage) http.HandleFunc("/aboutme", aboutMe) http.HandleFunc("/whoami", whoAmI) + http.HandleFunc("/ping", ping) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..c9e752e --- /dev/null +++ b/main_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +// TestPingEndpoint tests that the /ping endpoint returns "pong" +func TestPingEndpoint(t *testing.T) { + // Create a request to the /ping endpoint + req, err := http.NewRequest("GET", "/ping", nil) + if err != nil { + t.Fatal(err) + } + + // Create a ResponseRecorder to record the response + rr := httptest.NewRecorder() + + // Create a handler and serve the request + handler := http.HandlerFunc(ping) + handler.ServeHTTP(rr, req) + + // Check the status code + if status := rr.Code; status != http.StatusOK { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusOK) + } + + // Check the response body + expected := "pong" + if rr.Body.String() != expected { + t.Errorf("handler returned unexpected body: got %v want %v", + rr.Body.String(), expected) + } +} + +// Made with Bob