Exemplo n.º 1
0
func TestWatch(t *testing.T) {
	server := firetest.New()
	server.Start()
	defer server.Close()

	fb := New(server.URL)

	notifications := make(chan Event)
	err := fb.Watch(notifications)
	assert.NoError(t, err)

	l := setupLargeResult()
	server.Set("/foo", l)

	select {
	case event, ok := <-notifications:
		assert.True(t, ok)
		assert.Equal(t, "put", event.Type)
		assert.Equal(t, "/", event.Path)
		assert.Nil(t, event.Data)
	case <-time.After(250 * time.Millisecond):
		require.FailNow(t, "did not receive a notification initial notification")
	}

	select {
	case event, ok := <-notifications:
		assert.True(t, ok)
		assert.Equal(t, "/foo", event.Path)
		assert.EqualValues(t, l, event.Data)
	case <-time.After(250 * time.Millisecond):
		require.FailNow(t, "did not receive a notification")
	}
}
Exemplo n.º 2
0
func TestUnauth(t *testing.T) {
	t.Parallel()
	server := firetest.New()
	server.Start()
	defer server.Close()

	server.RequireAuth(true)
	fb := New(server.URL)

	fb.params.Add("auth", server.Secret)
	fb.Unauth()
	err := fb.Value("")
	assert.Error(t, err)
}
Exemplo n.º 3
0
func TestAuth(t *testing.T) {
	t.Parallel()
	server := firetest.New()
	server.Start()
	defer server.Close()

	server.RequireAuth(true)
	fb := New(server.URL)

	fb.Auth(server.Secret)
	var v interface{}
	err := fb.Value(&v)
	assert.NoError(t, err)
}
Exemplo n.º 4
0
func TestRemove(t *testing.T) {
	t.Parallel()
	server := firetest.New()
	server.Start()
	defer server.Close()

	server.Set("", true)

	fb := New(server.URL)
	err := fb.Remove()
	assert.NoError(t, err)

	v := server.Get("")
	assert.Nil(t, v)
}
Exemplo n.º 5
0
func TestSet(t *testing.T) {
	t.Parallel()
	var (
		payload = map[string]interface{}{"foo": "bar"}
		server  = firetest.New()
	)
	server.Start()
	defer server.Close()

	fb := New(server.URL, nil)
	err := fb.Set(payload)
	assert.NoError(t, err)

	v := server.Get("")
	assert.Equal(t, payload, v)
}
Exemplo n.º 6
0
func TestPush(t *testing.T) {
	t.Parallel()
	var (
		payload = map[string]interface{}{"foo": "bar"}
		server  = firetest.New()
	)
	server.Start()
	defer server.Close()

	fb := New(server.URL)
	childRef, err := fb.Push(payload)
	assert.NoError(t, err)

	path := strings.TrimPrefix(childRef.String(), server.URL+"/")
	v := server.Get(path)
	assert.Equal(t, payload, v)
}
Exemplo n.º 7
0
func TestValue(t *testing.T) {
	t.Parallel()
	var (
		response = map[string]interface{}{"foo": "bar"}
		server   = firetest.New()
	)
	server.Start()
	defer server.Close()

	fb := New(server.URL, nil)

	server.Set("", response)

	var v map[string]interface{}
	err := fb.Value(&v)
	assert.NoError(t, err)
	assert.Equal(t, response, v)
}
Exemplo n.º 8
0
func TestStopWatch(t *testing.T) {
	t.Parallel()

	server := firetest.New()
	server.Start()
	defer server.Close()

	fb := New(server.URL)

	notifications := make(chan Event)
	go func() {
		err := fb.Watch(notifications)
		assert.NoError(t, err)
	}()

	<-notifications // get initial notification
	fb.StopWatching()
	_, ok := <-notifications
	assert.False(t, ok, "notifications should be closed")
}
Exemplo n.º 9
0
func Example() {
	ft := firetest.New()
	defer ft.Close()

	ft.Start()

	resp, err := http.Post(ft.URL+"/foo.json", "application/json", strings.NewReader(`{"bar":true}`))
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Post Resp: %s\n", string(b))

	v := ft.Get("foo/bar")
	log.Printf("Value: %v", v)
}