docs(pier,seaglass,sunrise,sunscreen,tide): compile README Go examples via src= (quick-261118-qsd)
This commit is contained in:
@@ -71,15 +71,7 @@ Update mode takes no other argument. It works the same from bash, zsh and fish,
|
||||
|
||||
Serving the script from a Go handler:
|
||||
|
||||
```go
|
||||
package acme
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/sunrise"
|
||||
)
|
||||
|
||||
```go src=modules/sunrise/example_test.go#newScript
|
||||
func newScript(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
|
||||
35
modules/sunrise/example_test.go
Normal file
35
modules/sunrise/example_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package sunrise_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/sunrise"
|
||||
)
|
||||
|
||||
func newScript(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
_, _ = w.Write(sunrise.Script())
|
||||
}
|
||||
|
||||
func Example() {
|
||||
srv := httptest.NewServer(http.HandlerFunc(newScript))
|
||||
defer srv.Close()
|
||||
|
||||
resp, err := http.Get(srv.URL + "/new")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var body bytes.Buffer
|
||||
_, _ = body.ReadFrom(resp.Body)
|
||||
fmt.Println(resp.StatusCode, resp.Header.Get("Content-Type"))
|
||||
fmt.Println(bytes.Equal(body.Bytes(), sunrise.Script()), bytes.HasPrefix(body.Bytes(), []byte("#!")))
|
||||
// Output:
|
||||
// 200 text/plain; charset=utf-8
|
||||
// true true
|
||||
}
|
||||
Reference in New Issue
Block a user