package request
import (
"testing"
"time"
"fmt"
"encoding/xml"
)
var _ = fmt.Println
func TestLoginReqData(t *testing.T) {
req := NewLoginRequest("demouser1", "demo1Pass", ProductType.CFD_DEMO)
reqData := req.GetRequestData().String()
if reqData != "demouser1demo1Pass1.8CFD_DEMO" {
t.Errorf("Login RequestData Error:", reqData)
}
}
func TestOrderBookSubscriptReqData(t *testing.T) {
req := NewOrderBookSubscriptionRequest(4002)
reqData := req.GetRequestData().String()
if req.Url() != "/secure/subscribe" {
t.Errorf("SubRequest Url Error:")
}
req.SetUrl("/secure/unsubscribe")
if req.Url() != "/secure/unsubscribe" {
t.Errorf("UnSubRequest Url Error:")
}
if reqData != "4002" {
t.Errorf("SubRequest Error:", reqData)
}
}
func TestOrderSubscriptReqData(t *testing.T) {
req := NewOrderSubscriptionRequest()
reqData := req.GetRequestData().String()
if reqData != "order" {
t.Errorf("SubRequest Error:", reqData)
}
}
func TestAccountSubscriptReqData(t *testing.T) {
req := NewAccountSubscriptionRequest()
reqData := req.GetRequestData().String()
if reqData != "account" {
t.Errorf("AccountRequest Error:", reqData)
}
}
func TestPositionSubscriptReqData(t *testing.T) {
req := NewPositionSubscriptionRequest()
reqData := req.GetRequestData().String()
if reqData != "position" {
t.Errorf("PositionRequest Error:", reqData)
}
}
func TestOrderBookStatusReqData(t *testing.T) {
req := NewOrderBookStatusRequest(1003)
reqData := req.GetRequestData().String()
if reqData != "1003" {
t.Errorf("SubRequest Error:", reqData)
}
if req.Url() != "/secure/subscribe" {
t.Errorf("SubRequest Url Error:" + req.Url())
}
}
func TestMarketOrderReqData(t *testing.T) {
//var d float64 = 100
req := NewMarketOrderRequest(4001, "", TimeInForce.FILL_OR_KILL, 10, 0, 15)
reqData := req.GetRequestData().String()
if reqData != "400110FillOrKill15" {
t.Errorf("MarketOrder Error:", reqData)
}
if req.Quantity != 10 {
t.Errorf("MarketOrder.Quantity Error:", req.Price)
}
if req.StopLossPriceOffset != 0 {
t.Error("MarketOrder.StopLoss Error:", req.StopLossPriceOffset)
}
}
func TestCancelOrderReqData(t *testing.T) {
req := NewCancelOrderRequest(1003, "123", "")
reqData := req.GetRequestData().String()
if reqData != "1003123" {
t.Errorf("CancelRequest Error:", reqData)
}
}
func TestAmendOrderReqData(t *testing.T) {
req := NewAmendStopsOrderRequest(4001, "1235", "", 20, 26)
reqData := req.GetRequestData().String()
if reqData != "400112352026" {
t.Errorf("AmendRequest Error:", reqData)
}
}
func TestTopOfHistoricReqData(t *testing.T) {
req := NewTopOfBookHistoricRequest("1", 4001, time.Date(2011, 5, 11, 0, 0, 0, 0, time.UTC), time.Date(2011, 6, 13, 0, 0, 0, 0, time.UTC), "CSV")
reqData := req.GetRequestData().String()
if reqData != "14001130507200000013079232000001CSV" {
t.Errorf("TopOfHistoric Error: %s", reqData)
}
}
func TestAggregateHistoricReqData(t *testing.T) {
req := NewAggregateHistoricRequest("1", 4001, time.Date(2011, 5, 11, 0, 0, 0, 0, time.UTC), time.Date(2011, 6, 13, 0, 0, 0, 0, time.UTC), "CSV", "DAY", []string{"BID"})
reqData := req.GetRequestData().String()
if reqData != "1400113050720000001307923200000DAY1CSV" {
t.Errorf("AggregateHistoric Error:", reqData)
}
}
func TestHeartBeat(t *testing.T) {
heart := NewHeartbeatRequest("hello")
data := heart.GetRequestData().String()
if data != "hello" {
t.Error("TestHeartBeat error", data)
return
}
}
func TestLogoutXml(t *testing.T) {
heart := LogoutRequest{xml.Name{"", "req"}, ""}
data, err := xml.Marshal(heart)
if err != nil {
t.Error(err)
return
}
heart2 := LogoutRequest{}
err = xml.Unmarshal(data, &heart2)
if err != nil {
t.Error(err)
return
}
if heart != heart2 {
t.Error("LogoutRequest xml encoding/decoding error")
}
}