I want to make a simple server that can receive web hook requests from services and deal with them for me. And for fun I wanted to build that in Go, since that sounds like a nice language and this is a simple project to start with.
The server seems to be working fine, but I can't get my unittest to work. On inspection it seems that every url gives a 404. What am I doing wrong?
main.go
package main
import (
"fmt"
"log"
"net/http"
)
func pingHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{\"check\": \"online\"}")
}
func main() {
// Start server
log.Print("Starting server")
http.HandleFunc("/ping", pingHandler)
log.Fatal(http.ListenAndServe(":7080", nil))
}
main_test.go
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestPingHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/ping", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
http.DefaultServeMux.ServeHTTP(rr, req)
status := rr.Code
fmt.Println(status)
}