httpdir - example_test.go
1 package httpdir_test
2
3 import (
4 "fmt"
5 "io"
6 "net/http"
7 "net/http/httptest"
8 "os"
9 "time"
10
11 "vimagination.zapto.org/httpdir"
12 )
13
14 func Example() {
15 dir := httpdir.New(time.Now())
16
17 dir.Create("index.html", httpdir.FileString("<html><head><title>Example</title></head><body><h1>Hello from httpdir!</h1></body></html>", time.Now()))
18 dir.Create("style.css", httpdir.FileString("body { background: #f0f0f0; }", time.Now()))
19
20 http.Handle("/", http.FileServer(dir))
21
22 srv := httptest.NewServer(http.DefaultServeMux)
23
24 resp, err := srv.Client().Get(srv.URL + "/")
25 if err != nil {
26 fmt.Println(err)
27
28 return
29 }
30
31 io.Copy(os.Stdout, resp.Body)
32
33 // Output:
34 // <html><head><title>Example</title></head><body><h1>Hello from httpdir!</h1></body></html>
35 }
36