initial enclosure support
continuous-integration/drone/push Build is failing Details

main
Drew Bednar 2 months ago
parent c3bb2b3475
commit 5d78ee184e

@ -1,16 +1,14 @@
package main package main
import ( import (
"fmt" "log"
"io" "net/http"
"os"
)
func printMessage(w io.Writer, msg string) { "git.runcible.io/learning/bugbox"
fmt.Fprint(w, msg) )
}
func main() { func main() {
log.Println("Serving on port: 8888")
printMessage(os.Stdout, "Bugbox server\n") handler := http.HandlerFunc(bugbox.BugBoxServer)
log.Fatal(http.ListenAndServe(":8888", handler))
} }

@ -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)
}
}

@ -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))
}

@ -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")
})
}
Loading…
Cancel
Save