Esempio n. 1
0
func (t caseTest) TestACK() error {
	origin, err := testServer.Origin()
	if err != nil {
		return fmt.Errorf("On test %v, error initializing test server: %#v", t.CaseTestType, err)
	}
	conn, err := client.DialOrigin(origin)
	if err != nil {
		return fmt.Errorf("On test %v, error dialing origin: %#v", t.CaseTestType, err)
	}
	defer conn.Close()
	defer conn.Purge()
	conn.RegisterDecoder("ack", client.DecoderFunc(decodeCaseACK))
	request := CaseACK{t.CaseTestType, client.NewACK(nil, true)}
	_, err = conn.WriteRequest(request)
	if t.statusCode >= 200 && t.statusCode < 300 {
		if err != nil {
			return fmt.Errorf("On test %v, error writing acknowledgement: %#v", t.CaseTestType, err)
		}
		return nil
	}
	if err != io.EOF {
		return fmt.Errorf("On test %v, error writing acknowledgement: got %#v; want io.EOF", t.CaseTestType, err)
	}
	err = conn.Close()
	clientErr, ok := err.(client.Error)
	if !ok {
		return fmt.Errorf("On test %v, type assertion failed for close error: %#v", t.CaseTestType, err)
	}
	if clientErr.Status() != t.statusCode {
		return fmt.Errorf("On test %v, unexpected close error status: got %#v; want %#v", t.CaseTestType, clientErr.Status(), t.statusCode)
	}
	return nil
}
Esempio n. 2
0
func TestUnregister(t *testing.T) {
	origin, err := testServer.Origin()
	if err != nil {
		t.Fatalf("Error initializing test server: %#v", err)
	}
	conn, _, err := client.Dial(origin)
	if err != nil {
		t.Fatalf("Error dialing origin: %#v", err)
	}
	defer conn.Close()
	defer conn.Purge()
	channelId, _, err := conn.Subscribe()
	if err != nil {
		t.Fatalf("Error subscribing to channel: %#v", err)
	}
	conn.RegisterDecoder("unregister", client.DecoderFunc(decodeUnregisterReply))
	for index := 0; index < 2; index++ {
		request := client.NewUnregister(channelId, true)
		reply, err := conn.WriteRequest(request)
		if err != nil {
			t.Fatalf("Error writing deregistration request (attempt %d): %#v", index, err)
		}
		if reply.Status() != 200 {
			t.Errorf("Unexpected status code for deregistration attempt %d: got %#v, want 200", index, reply.Status())
		}
	}
}
Esempio n. 3
0
func TestPrematureUnregister(t *testing.T) {
	channelId, err := id.Generate()
	if err != nil {
		t.Fatalf("Error generating channel ID: %#v", err)
	}
	origin, err := testServer.Origin()
	if err != nil {
		t.Fatalf("Error initializing test server: %#v", err)
	}
	conn, err := client.DialOrigin(origin)
	if err != nil {
		t.Fatalf("Error dialing origin: %#v", err)
	}
	defer conn.Close()
	defer conn.Purge()
	conn.RegisterDecoder("unregister", client.DecoderFunc(decodeUnregisterReply))
	request := client.NewUnregister(channelId, true)
	_, err = conn.WriteRequest(request)
	if err != io.EOF {
		t.Fatalf("Error writing deregistration request: got %#v; want io.EOF", err)
	}
	err = conn.Close()
	clientErr, ok := err.(client.Error)
	if !ok {
		t.Fatalf("Type assertion failed for close error: %#v", err)
	}
	if clientErr.Status() != 401 {
		t.Errorf("Unexpected close error status: got %#v; want 401", clientErr.Status())
	}
}
Esempio n. 4
0
func TestPrematureACK(t *testing.T) {
	channelId, err := id.Generate()
	if err != nil {
		t.Fatalf("Error generating channel ID: %#v", err)
	}
	origin, err := testServer.Origin()
	if err != nil {
		t.Fatalf("Error initializing test server: %#v", err)
	}
	conn, err := client.DialOrigin(origin)
	if err != nil {
		t.Fatalf("Error dialing origin: %#v", err)
	}
	defer conn.Close()
	defer conn.Purge()
	conn.RegisterDecoder("ack", client.DecoderFunc(decodeServerInvalidACK))
	updates := []client.Update{
		client.Update{
			ChannelId: channelId,
			Version:   time.Now().UTC().Unix(),
		},
	}
	request := ClientInvalidACK{client.NewACK(updates, true)}
	reply, err := conn.WriteRequest(request)
	if err != nil {
		t.Fatalf("Error writing acknowledgement: %#v", err)
	}
	if reply.Status() != 401 {
		t.Errorf("Incorrect status code: got %#v, wanted 401", reply.Status())
	}
	if r, ok := reply.(ServerInvalidACK); ok {
		if len(r.Updates) != len(updates) {
			t.Errorf("Incorrect update count: got %#v; want %#v", len(r.Updates), len(updates))
		} else {
			for index, update := range r.Updates {
				if update != updates[index] {
					t.Errorf("On update %#v, got %#v; want %#v", index, update, updates[index])
				}
			}
		}
	} else {
		t.Errorf("Type assertion failed for reply: %#v", reply)
	}
	// The connection should be closed by the push server after sending
	// the error response.
	if err = conn.Close(); err != io.EOF {
		t.Fatalf("Unexpected close error: got %#v; want io.EOF", err)
	}
}
Esempio n. 5
0
func (t caseTest) TestPing() error {
	origin, err := testServer.Origin()
	if err != nil {
		return fmt.Errorf("On test %v, error initializing test server: %#v", t.CaseTestType, err)
	}
	conn, err := client.DialOrigin(origin)
	if err != nil {
		return fmt.Errorf("On test %v, error dialing origin: %#v", t.CaseTestType, err)
	}
	defer conn.Close()
	defer conn.Purge()
	conn.RegisterDecoder("ping", client.DecoderFunc(decodePing))
	request := CaseClientPing{t.CaseTestType, client.NewPing(true)}
	reply, err := conn.WriteRequest(request)
	if err != nil {
		return fmt.Errorf("On test %v, error writing ping packet: %#v", t.CaseTestType, err)
	}
	if reply.Status() != t.statusCode {
		return fmt.Errorf("On test %v, unexpected status code: got %#v; want %#v", t.CaseTestType, reply.Status(), t.statusCode)
	}
	return nil
}
Esempio n. 6
0
func TestPing(t *testing.T) {
	origin, err := testServer.Origin()
	if err != nil {
		t.Fatalf("Error initializing test server: %#v", err)
	}
	conn, _, err := client.Dial(origin)
	if err != nil {
		t.Fatalf("Error dialing origin: %#v", err)
	}
	defer conn.Close()
	defer conn.Purge()
	conn.RegisterDecoder("ping", client.DecoderFunc(decodePing))
	var pingWait sync.WaitGroup
	writePing := func() {
		defer pingWait.Done()
		reply, err := conn.WriteRequest(client.NewPing(true))
		if err != nil {
			t.Errorf("Error writing ping request: %#v", err)
			return
		}
		if reply.Status() != 200 {
			t.Errorf("Unexpected ping reply status code: got %#v; want 200", reply.Status())
		}
	}
	writeRegister := func() {
		defer pingWait.Done()
		channelId, endpoint, err := conn.Subscribe()
		if err != nil {
			t.Errorf("Error subscribing to channel: %#v", err)
		}
		if !isValidEndpoint(endpoint) {
			t.Errorf("Invalid push endpoint for channel %#v: %#v", channelId, endpoint)
		}
	}
	pingWait.Add(2)
	go writeRegister()
	go writePing()
	pingWait.Wait()
}