Ejemplo n.º 1
0
func TestSpriteHTTP(t *testing.T) {
	in := bytes.NewBufferString(`
$map: sprite-map("*.png", 10px);
div {
  background: sprite($map, "140");
}`)

	var out bytes.Buffer
	comp, err := libsass.New(&out, in,
		libsass.OutputStyle(libsass.NESTED_STYLE),
		libsass.BuildDir("../test/build"),
		libsass.ImgDir("../test/img"),
		libsass.FontDir("../test/font"),
		libsass.Payload(payload.New()),
		libsass.ImgBuildDir("../test/build/img"),
		libsass.HTTPPath("http://foo.com"),
	)
	if err != nil {
		t.Fatal(err)
	}

	if err := comp.Run(); err != nil {
		t.Fatal(err)
	}

	e := `div {
  background: url("http://foo.com/build/b9d55f.png") 0px -149px; }
`
	if e != out.String() {
		t.Errorf("got:\n%s\nwanted:\n%s", out.String(), e)
	}

}
Ejemplo n.º 2
0
// HTTPHandler starts a CORS enabled web server that takes as input
// Sass and outputs CSS.
func HTTPHandler(gba *BuildArgs, httpPath string) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		setDefaultHeaders(w, r)
		start := time.Now()
		resp := Response{
			Start:   start,
			Version: version.Version,
		}
		var (
			err  error
			pout bytes.Buffer
		)
		enc := json.NewEncoder(w)
		defer func() {
			resp.Contents = pout.String()
			resp.Elapsed = time.Since(start).String()
			if err != nil {
				resp.Error = err.Error()
			}
			enc.Encode(resp)
		}()
		if r.Body == nil {
			err = errors.New("request is empty")
			return
		}
		defer r.Body.Close()

		comp, err := FromBuildArgs(&pout, nil, r.Body, gba)
		if err != nil {
			resp.Contents = ""
			return
		}
		comp.Option(libsass.HTTPPath(httpPath))
		err = comp.Run()
	}
}