Exemplo n.º 1
0
// Connect to remote instance of Redis using a URL.
func ExampleDialURL() {
	c, err := redis.DialURL(os.Getenv("REDIS_URL"))
	if err != nil {
		// handle connection error
	}
	defer c.Close()
}
Exemplo n.º 2
0
func TestDialURLErrors(t *testing.T) {
	for _, d := range dialErrors {
		_, err := redis.DialURL(d.rawurl)
		if err == nil || !strings.Contains(err.Error(), d.expectedError) {
			t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError)
		}
	}
}
Exemplo n.º 3
0
func TestDialURLDatabase(t *testing.T) {
	var buf bytes.Buffer
	_, err := redis.DialURL("redis://localhost/3", dialTestConn(strings.NewReader("+OK\r\n"), &buf))
	if err != nil {
		t.Error("dial error:", err)
	}
	expected := "*2\r\n$6\r\nSELECT\r\n$1\r\n3\r\n"
	actual := buf.String()
	if actual != expected {
		t.Errorf("commands = %q, want %q", actual, expected)
	}
}
Exemplo n.º 4
0
func TestDialURLPassword(t *testing.T) {
	var buf bytes.Buffer
	_, err := redis.DialURL("redis://*****:*****@localhost", dialTestConn(strings.NewReader("+OK\r\n"), &buf))
	if err != nil {
		t.Error("dial error:", err)
	}
	expected := "*2\r\n$4\r\nAUTH\r\n$6\r\nabc123\r\n"
	actual := buf.String()
	if actual != expected {
		t.Errorf("commands = %q, want %q", actual, expected)
	}
}
Exemplo n.º 5
0
func TestDialURLHost(t *testing.T) {
	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return nil, nil
	}
	_, err := redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
}
Exemplo n.º 6
0
func TestDialURL(t *testing.T) {
	for _, d := range dialErrors {
		_, err := redis.DialURL(d.rawurl)
		if err == nil || !strings.Contains(err.Error(), d.expectedError) {
			t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError)
		}
	}

	checkPort := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set port to 6379 by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err := redis.DialURL("redis://localhost", redis.DialNetDial(checkPort))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err = redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	// Check that the database is set correctly
	c1, err := redis.DialURL("redis://:6379/8")
	defer c1.Close()
	if err != nil {
		t.Error("Dial error:", err)
	}
	dbSize, _ := redis.Int(c1.Do("DBSIZE"))
	if dbSize > 0 {
		t.Fatal("DB 8 has existing keys; aborting test to avoid overwriting data")
	}
	c1.Do("SET", "var", "val")

	c2, err := redis.Dial("tcp", ":6379")
	defer c2.Close()
	if err != nil {
		t.Error("dial error:", err)
	}
	_, err = c2.Do("SELECT", "8")
	if err != nil {
		t.Error(err)
	}
	got, err := redis.String(c2.Do("GET", "var"))
	if err != nil {
		t.Error(err)
	}
	if got != "val" {
		t.Error("DialURL did not correctly set the db.")
	}
	_, err = c2.Do("DEL", "var")
}