From 5d78ee184e6448c2074ce7c4e2b9f5f06f9a4726 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 16 Nov 2024 15:09:32 -0500 Subject: [PATCH] initial enclosure support --- cmd/bugbox/main.go | 16 +++++++-------- cmd/bugbox/main_test.go | 18 ----------------- server.go | 17 ++++++++++++++++ server_test.go | 45 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 27 deletions(-) delete mode 100644 cmd/bugbox/main_test.go create mode 100644 server.go create mode 100644 server_test.go diff --git a/cmd/bugbox/main.go b/cmd/bugbox/main.go index 52841f2..132579c 100644 --- a/cmd/bugbox/main.go +++ b/cmd/bugbox/main.go @@ -1,16 +1,14 @@ package main import ( - "fmt" - "io" - "os" -) + "log" + "net/http" -func printMessage(w io.Writer, msg string) { - fmt.Fprint(w, msg) -} + "git.runcible.io/learning/bugbox" +) func main() { - - printMessage(os.Stdout, "Bugbox server\n") + log.Println("Serving on port: 8888") + handler := http.HandlerFunc(bugbox.BugBoxServer) + log.Fatal(http.ListenAndServe(":8888", handler)) } diff --git a/cmd/bugbox/main_test.go b/cmd/bugbox/main_test.go deleted file mode 100644 index 9af04a6..0000000 --- a/cmd/bugbox/main_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package main - -import ( - "bytes" - "testing" -) - -func TestMain(t *testing.T) { - msg := "Bug box server\n" - buffer := bytes.Buffer{} - printMessage(&buffer, msg) - got := buffer.String() - want := msg - - if got != want { - t.Errorf("Error, got %s, want %s", got, want) - } -} diff --git a/server.go b/server.go new file mode 100644 index 0000000..383f8eb --- /dev/null +++ b/server.go @@ -0,0 +1,17 @@ +package bugbox + +import ( + "fmt" + "net/http" + "strings" +) + +// GetEnclosure retrieves the requested enclosure +func GetEnclosure(name string) string { + return name +} + +func BugBoxServer(w http.ResponseWriter, r *http.Request) { + enclosure := strings.TrimPrefix(r.URL.Path, "/enclosure/") + fmt.Fprint(w, GetEnclosure(enclosure)) +} diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..ef88f9f --- /dev/null +++ b/server_test.go @@ -0,0 +1,45 @@ +package bugbox + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func newGETEnclosureRequest(name string) *http.Request { + request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/enclosure/%s", name), nil) + return request +} + +func assertResponseBody(t testing.TB, got, want string) { + t.Helper() + if got != want { + // a %q is a double quoted string + t.Errorf("got %q, want %q", got, want) + } +} + +func TestGETEnclosures(t *testing.T) { + t.Run("returns enclosure 1337", func(t *testing.T) { + // nil is used since we are not providing a request body + request := newGETEnclosureRequest("1337") + // response is a ResponseRecorder used for spying on response + response := httptest.NewRecorder() + + BugBoxServer(response, request) + + assertResponseBody(t, response.Body.String(), "1337") + + }) + t.Run("returns enclosure 7331", func(t *testing.T) { + // nil is used since we are not providing a request body + request := newGETEnclosureRequest("7331") + // response is a ResponseRecorder used for spying on response + response := httptest.NewRecorder() + + BugBoxServer(response, request) + + assertResponseBody(t, response.Body.String(), "7331") + }) +}