Skip to content
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
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -52,14 +54,51 @@ 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)
output.WriteString("!\n")

_, 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
}
}
61 changes: 61 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Loading