Exemplo 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
}
Exemplo n.º 2
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)
	}
}