Last active
June 25, 2019 19:38
-
-
Save tiloso/2fc36763626cb9f11791017b1fd2a538 to your computer and use it in GitHub Desktop.
Simple Go Webserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
"github.com/go-chi/chi" | |
) | |
func main() { | |
log.Fatal(http.ListenAndServe(":8080", handler())) | |
} | |
func handler() http.Handler { | |
r := chi.NewRouter() | |
r.Get("/", func(w http.ResponseWriter, r *http.Request) { | |
io.WriteString(w, "🚀") | |
}) | |
return r | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
"github.com/google/go-cmp/cmp" | |
) | |
func TestHandler(t *testing.T) { | |
ts := httptest.NewServer(handler()) | |
defer ts.Close() | |
res, err := http.Get(ts.URL) | |
if err != nil { | |
t.Fatal(err) | |
} | |
have, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
t.Fatal(err) | |
} | |
want := []byte("🚀") | |
if !cmp.Equal(have, want) { | |
t.Errorf("%v", cmp.Diff(have, want)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment