diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c64b9bf --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,31 @@ +name: test + +on: + push: + branches: + - master + - main + pull_request: + +jobs: + build-and-test: + runs-on: ubuntu-latest + + steps: + + - name: checkout code + uses: actions/checkout@v6 + + - name: Set up go + uses: actions/setup-go@v6 + with: + go-version: "1.25" + + - name: Download Dependencies + run: go mod download + +# - name: Build +# run: go Build + + - name: Run tests + run: go test ./... \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/main.go b/main.go index 0faa390..5fe8ea5 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,11 @@ import ( func main() { mux := http.NewServeMux() - mux.HandleFunc("/", handleRoot) + mux.HandleFunc("/{$}", handleRoot) mux.HandleFunc("/goodbye", handleGoodbye) mux.HandleFunc("/hello/", handleHelloParameterized) + mux.HandleFunc("/responses/{user}/hello/", handleUserResponsesHello) + mux.HandleFunc("/user/hello", handleHelloHeader) fmt.Println("Listening on port 4000") @@ -52,6 +54,43 @@ func handleHelloParameterized(w http.ResponseWriter, r *http.Request) { username = userlist[0] } + handleHello(w, username) +} + +func handleUserResponsesHello(w http.ResponseWriter, r *http.Request) { + fmt.Println("Requested Path: ", r.URL.Path) + + username := r.PathValue("user") + + handleHello(w, username) +} + +func handleHelloHeader(w http.ResponseWriter, r *http.Request) { + fmt.Println("Requested Path: ", r.URL.Path) + //username := r.PathValue("user") + username := r.Header.Get("user") + if username == "" { + http.Error(w, "invalid username provided", http.StatusBadRequest) + return + } + + handleHello(w, username) +} + +func handleHelloNoHeader(w http.ResponseWriter, r *http.Request) { + fmt.Println("Requested Path: ", r.URL.Path) + //username := r.PathValue("user") + username := r.Header.Get("user") + if username == "" { + http.Error(w, "invalid username provided", http.StatusBadRequest) + return + } + + handleHello(w, username) +} + +func handleHello(w http.ResponseWriter, username string) { + var output bytes.Buffer output.WriteString("Hello ") output.WriteString(username) @@ -59,7 +98,7 @@ func handleHelloParameterized(w http.ResponseWriter, r *http.Request) { _, err := w.Write(output.Bytes()) if err != nil { - slog.Error("error starting response body", "err", err.Error()) + slog.Error("error starting response body", "err: ", err.Error()) return } } diff --git a/main_test.go b/main_test.go index 134761b..a54c89f 100644 --- a/main_test.go +++ b/main_test.go @@ -99,3 +99,64 @@ func TestHandleHelloWrongParameterized(t *testing.T) { string(expectedMessage), string(w.Body.Bytes()), w.Body.String()) } } + +func TestHandleUserResponsesHello(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, "/responses/TestMan/hello", nil) + r.SetPathValue("user", "TestMan") + w := httptest.NewRecorder() + + handleUserResponsesHello(w, r) + + desiredCode := http.StatusOK + if w.Code != desiredCode { + t.Errorf("bad response code: expected %d, got %d\nbody: %s\n", + desiredCode, w.Code, w.Body.String()) + } + + expectedMessage := []byte("Hello TestMan!\n") + if !bytes.Equal(w.Body.Bytes(), expectedMessage) { + t.Errorf("bad response body: expected %s, got %s\nbody: %s\n", + string(expectedMessage), string(w.Body.Bytes()), w.Body.String()) + } +} + +func TestHandleHelloHeader(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, "/user/hello", nil) + r.Header.Set("user", "TestMan") + + w := httptest.NewRecorder() + + handleHelloHeader(w, r) + + desiredCode := http.StatusOK + if w.Code != desiredCode { + t.Errorf("bad response code: expected %d, got %d\nbody: %s\n", + desiredCode, w.Code, w.Body.String()) + } + + expectedMessage := []byte("Hello TestMan!\n") + if !bytes.Equal(w.Body.Bytes(), expectedMessage) { + t.Errorf("bad response body: expected %s, got %s\nbody: %s\n", + string(expectedMessage), string(w.Body.Bytes()), w.Body.String()) + } +} + +func TestHandleHelloNoHeader(t *testing.T) { + r := httptest.NewRequest(http.MethodGet, "/user/hello", nil) + + w := httptest.NewRecorder() + + handleHelloNoHeader(w, r) + + desiredCode := http.StatusBadRequest + if w.Code != desiredCode { + t.Errorf("bad response code: expected %d, got %d\nbody: %s\n", + desiredCode, w.Code, w.Body.String()) + } + + expectedMessage := []byte("invalid username provided\n") + if !bytes.Equal(w.Body.Bytes(), expectedMessage) { + t.Errorf("bad response body: expected %s, got %s\nbody: %s\n", + string(expectedMessage), string(w.Body.Bytes()), w.Body.String()) + } +}