123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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 != "<req><body><username>demouser1</username><password>demo1Pass</password><protocolVersion>1.8</protocolVersion><productType>CFD_DEMO</productType></body></req>" {
- 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 != "<req><body><subscription><ob2>4002</ob2></subscription></body></req>" {
- t.Errorf("SubRequest Error:", reqData)
- }
- }
- func TestOrderSubscriptReqData(t *testing.T) {
- req := NewOrderSubscriptionRequest()
- reqData := req.GetRequestData().String()
- if reqData != "<req><body><subscription><type>order</type></subscription></body></req>" {
- t.Errorf("SubRequest Error:", reqData)
- }
- }
- func TestAccountSubscriptReqData(t *testing.T) {
- req := NewAccountSubscriptionRequest()
- reqData := req.GetRequestData().String()
- if reqData != "<req><body><subscription><type>account</type></subscription></body></req>" {
- t.Errorf("AccountRequest Error:", reqData)
- }
- }
- func TestPositionSubscriptReqData(t *testing.T) {
- req := NewPositionSubscriptionRequest()
- reqData := req.GetRequestData().String()
- if reqData != "<req><body><subscription><type>position</type></subscription></body></req>" {
- t.Errorf("PositionRequest Error:", reqData)
- }
- }
- func TestOrderBookStatusReqData(t *testing.T) {
- req := NewOrderBookStatusRequest(1003)
- reqData := req.GetRequestData().String()
- if reqData != "<req><body><subscription><orderBookStatus>1003</orderBookStatus></subscription></body></req>" {
- 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 != "<req><body><order><instrumentId>4001</instrumentId><quantity>10</quantity><timeInForce>FillOrKill</timeInForce><stopProfitOffset>15</stopProfitOffset></order></body></req>" {
- 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 != "<req><body><instrumentId>1003</instrumentId><originalInstructionId>123</originalInstructionId></body></req>" {
- t.Errorf("CancelRequest Error:", reqData)
- }
- }
- func TestAmendOrderReqData(t *testing.T) {
- req := NewAmendStopsOrderRequest(4001, "1235", "", 20, 26)
- reqData := req.GetRequestData().String()
- if reqData != "<req><body><instrumentId>4001</instrumentId><originalInstructionId>1235</originalInstructionId><stopLossOffset>20</stopLossOffset><stopProfitOffset>26</stopProfitOffset></body></req>" {
- 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 != "<req><body><instructionId>1</instructionId><orderBookId>4001</orderBookId><from>1305072000000</from><to>1307923200000</to><orderBook><options><option>BID</option><option>ASK</option></options><depth>1</depth><format>CSV</format></orderBook></body></req>" {
- 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 != "<req><body><instructionId>1</instructionId><orderBookId>4001</orderBookId><from>1305072000000</from><to>1307923200000</to><aggregate><options><option>BID</option></options><resolution>DAY</resolution><depth>1</depth><format>CSV</format></aggregate></body></req>" {
- t.Errorf("AggregateHistoric Error:", reqData)
- }
- }
- func TestHeartBeat(t *testing.T) {
- heart := NewHeartbeatRequest("hello")
- data := heart.GetRequestData().String()
- if data != "<req><body><token>hello</token></body></req>" {
- 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")
- }
- }
|