Req_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package request
  2. import (
  3. "testing"
  4. "time"
  5. "fmt"
  6. "encoding/xml"
  7. )
  8. var _ = fmt.Println
  9. func TestLoginReqData(t *testing.T) {
  10. req := NewLoginRequest("demouser1", "demo1Pass", ProductType.CFD_DEMO)
  11. reqData := req.GetRequestData().String()
  12. if reqData != "<req><body><username>demouser1</username><password>demo1Pass</password><protocolVersion>1.8</protocolVersion><productType>CFD_DEMO</productType></body></req>" {
  13. t.Errorf("Login RequestData Error:", reqData)
  14. }
  15. }
  16. func TestOrderBookSubscriptReqData(t *testing.T) {
  17. req := NewOrderBookSubscriptionRequest(4002)
  18. reqData := req.GetRequestData().String()
  19. if req.Url() != "/secure/subscribe" {
  20. t.Errorf("SubRequest Url Error:")
  21. }
  22. req.SetUrl("/secure/unsubscribe")
  23. if req.Url() != "/secure/unsubscribe" {
  24. t.Errorf("UnSubRequest Url Error:")
  25. }
  26. if reqData != "<req><body><subscription><ob2>4002</ob2></subscription></body></req>" {
  27. t.Errorf("SubRequest Error:", reqData)
  28. }
  29. }
  30. func TestOrderSubscriptReqData(t *testing.T) {
  31. req := NewOrderSubscriptionRequest()
  32. reqData := req.GetRequestData().String()
  33. if reqData != "<req><body><subscription><type>order</type></subscription></body></req>" {
  34. t.Errorf("SubRequest Error:", reqData)
  35. }
  36. }
  37. func TestAccountSubscriptReqData(t *testing.T) {
  38. req := NewAccountSubscriptionRequest()
  39. reqData := req.GetRequestData().String()
  40. if reqData != "<req><body><subscription><type>account</type></subscription></body></req>" {
  41. t.Errorf("AccountRequest Error:", reqData)
  42. }
  43. }
  44. func TestPositionSubscriptReqData(t *testing.T) {
  45. req := NewPositionSubscriptionRequest()
  46. reqData := req.GetRequestData().String()
  47. if reqData != "<req><body><subscription><type>position</type></subscription></body></req>" {
  48. t.Errorf("PositionRequest Error:", reqData)
  49. }
  50. }
  51. func TestOrderBookStatusReqData(t *testing.T) {
  52. req := NewOrderBookStatusRequest(1003)
  53. reqData := req.GetRequestData().String()
  54. if reqData != "<req><body><subscription><orderBookStatus>1003</orderBookStatus></subscription></body></req>" {
  55. t.Errorf("SubRequest Error:", reqData)
  56. }
  57. if req.Url() != "/secure/subscribe" {
  58. t.Errorf("SubRequest Url Error:" + req.Url())
  59. }
  60. }
  61. func TestMarketOrderReqData(t *testing.T) {
  62. //var d float64 = 100
  63. req := NewMarketOrderRequest(4001, "", TimeInForce.FILL_OR_KILL, 10, 0, 15)
  64. reqData := req.GetRequestData().String()
  65. if reqData != "<req><body><order><instrumentId>4001</instrumentId><quantity>10</quantity><timeInForce>FillOrKill</timeInForce><stopProfitOffset>15</stopProfitOffset></order></body></req>" {
  66. t.Errorf("MarketOrder Error:", reqData)
  67. }
  68. if req.Quantity != 10 {
  69. t.Errorf("MarketOrder.Quantity Error:", req.Price)
  70. }
  71. if req.StopLossPriceOffset != 0 {
  72. t.Error("MarketOrder.StopLoss Error:", req.StopLossPriceOffset)
  73. }
  74. }
  75. func TestCancelOrderReqData(t *testing.T) {
  76. req := NewCancelOrderRequest(1003, "123", "")
  77. reqData := req.GetRequestData().String()
  78. if reqData != "<req><body><instrumentId>1003</instrumentId><originalInstructionId>123</originalInstructionId></body></req>" {
  79. t.Errorf("CancelRequest Error:", reqData)
  80. }
  81. }
  82. func TestAmendOrderReqData(t *testing.T) {
  83. req := NewAmendStopsOrderRequest(4001, "1235", "", 20, 26)
  84. reqData := req.GetRequestData().String()
  85. if reqData != "<req><body><instrumentId>4001</instrumentId><originalInstructionId>1235</originalInstructionId><stopLossOffset>20</stopLossOffset><stopProfitOffset>26</stopProfitOffset></body></req>" {
  86. t.Errorf("AmendRequest Error:", reqData)
  87. }
  88. }
  89. func TestTopOfHistoricReqData(t *testing.T) {
  90. 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")
  91. reqData := req.GetRequestData().String()
  92. 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>" {
  93. t.Errorf("TopOfHistoric Error: %s", reqData)
  94. }
  95. }
  96. func TestAggregateHistoricReqData(t *testing.T) {
  97. 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"})
  98. reqData := req.GetRequestData().String()
  99. 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>" {
  100. t.Errorf("AggregateHistoric Error:", reqData)
  101. }
  102. }
  103. func TestHeartBeat(t *testing.T) {
  104. heart := NewHeartbeatRequest("hello")
  105. data := heart.GetRequestData().String()
  106. if data != "<req><body><token>hello</token></body></req>" {
  107. t.Error("TestHeartBeat error", data)
  108. return
  109. }
  110. }
  111. func TestLogoutXml(t *testing.T) {
  112. heart := LogoutRequest{xml.Name{"", "req"}, ""}
  113. data, err := xml.Marshal(heart)
  114. if err != nil {
  115. t.Error(err)
  116. return
  117. }
  118. heart2 := LogoutRequest{}
  119. err = xml.Unmarshal(data, &heart2)
  120. if err != nil {
  121. t.Error(err)
  122. return
  123. }
  124. if heart != heart2 {
  125. t.Error("LogoutRequest xml encoding/decoding error")
  126. }
  127. }