initial enclosure support
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
c3bb2b3475
commit
5d78ee184e
@ -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))
|
||||
}
|
||||
|
@ -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…
Reference in New Issue