Example #1
0
func TestVarIndex(t *testing.T) {
	var seen bool
	h := func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		seen = true
	}
	m := darwini.Var{
		Index: darwini.HandlerFunc(h),
	}
	resp := DoRequest(m, "GET", "/", nil)
	if !seen {
		t.Errorf("never saw GET: %v %v", resp.Code, resp.Body)
	}
}
Example #2
0
func TestDirParent(t *testing.T) {
	var seen bool
	h := func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		seen = true
	}
	m := darwini.Map{
		"foo": darwini.Dir{
			Parent: darwini.HandlerFunc(h),
		},
	}
	resp := DoRequest(m, "GET", "/foo", nil)
	if !seen {
		t.Errorf("never saw GET: %v %v", resp.Code, resp.Body)
	}
}
Example #3
0
func TestVarChildDeep(t *testing.T) {
	var seen bool
	childFn := func(seg string) darwini.Handler {
		if g, e := seg, "foo"; g != e {
			t.Errorf("darwini.Var gave wrong string: %q != %q", g, e)
		}
		return darwini.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
			if g, e := req.URL.Path, "/bar/baz"; g != e {
				t.Errorf("darwini.Var child url path is wrong: %q != %q", g, e)
			}
			seen = true
		})
	}
	m := darwini.Var{
		Child: childFn,
	}
	resp := DoRequest(m, "GET", "/foo/bar/baz", nil)
	if !seen {
		t.Errorf("never saw GET: %v %v", resp.Code, resp.Body)
	}
}
Example #4
0
func Example() {
	gists := &gistStore{
		gists: map[uint64]*gist{},
	}
	m := darwini.Map{
		"gists": darwini.Dir{
			Parent: darwini.Method{
				GET:  gists.list,
				POST: gists.create,
			},
			Child: darwini.Var{
				Child: func(seg string) darwini.Handler {
					// Mixing dynamic and static segments is just bad,
					// so we won't bother to assist in that. Write code.
					switch seg {
					case "public":
						return darwini.HandlerFunc(gists.listPublic)
					case "starred":
						return darwini.HandlerFunc(gists.listStarred)
					}
					g, err := gists.get(seg)
					if err != nil {
						return nil // not found
					}
					return darwini.Dir{
						Parent: darwini.Method{
							GET:    g.get,
							PATCH:  g.patch,
							DELETE: g.del,
						},
						Child: darwini.Map{
							"": darwini.HandlerFunc(removeSlash),
							"star": darwini.Method{
								GET:    g.isStar,
								PUT:    g.star,
								DELETE: g.unstar,
							},
							"forks": darwini.Method{
								GET: g.forks,
							},
						},
					}
				},
			},
		},
	}
	s := httptest.NewServer(darwini.WithContext(m))
	defer s.Close()

	resp, err := http.Post(s.URL+"/gists", "text/plain", strings.NewReader("hello, world"))
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		fmt.Println(resp.Status)
		return
	}
	var id uint64
	if err := json.NewDecoder(resp.Body).Decode(&id); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("created gist", id)

	resp, err = http.Get(s.URL + "/gists/" + strconv.FormatUint(id, 10) + "/star")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		fmt.Println(resp.Status)
		return
	}
	var starred bool
	if err := json.NewDecoder(resp.Body).Decode(&starred); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("gist %d is starred: %v\n", id, starred)

	// Output:
	// created gist 1
	// gist 1 is starred: false
}