package tick import "testing" import "log" import "net/http" import "encoding/json" import "bytes" import "io/ioutil" import "errors" import "io" import "time" import "github.com/niniwzw/http2" import "fmt" var _ = log.Println var isssl = true var client *http.Client func init() { if isssl { client = &http.Client{Transport: tr} } else { client = &http.Client{} } } //获取sessionId func httpreq(name string, req interface{}) ([]byte, error) { s, err := json.Marshal(req) if err != nil { return nil, err } var url = "https://localhost:2107/" if !isssl { url = "http://localhost:2107/" } resp, err := client.Post(url+name, "text/json", bytes.NewBuffer(s)) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } log.Println(string(body)) return body, nil } func httpreqstream(name string, req interface{}) (io.ReadCloser, error) { s, err := json.Marshal(req) if err != nil { return nil, err } var url = "https://localhost:2107/" if !isssl { url = "http://localhost:2107/" } resp, err := client.Post(url+name, "text/json", bytes.NewBuffer(s)) if err != nil { return nil, err } return resp.Body, err } type JsonResponse2 struct { Result *json.RawMessage `json:"result"` Err string `json:"err"` Code int `json:"code"` } func decodeResponse(datain []byte, dataout interface{}) (int, error) { var resp JsonResponse2 err := json.Unmarshal(datain, &resp) if err != nil { return -1, err } if resp.Err != "" { return -1, errors.New(resp.Err) } if resp.Result == nil { return 0, nil } json.Unmarshal(*resp.Result, &dataout) return 0, nil } func TestTicks(t *testing.T) { req := &TypeRequest{Type: "sina"} body, err := httpreq("ticks", req) if err != nil { t.Error(err) } var ticks []*Market _, err = decodeResponse(body, &ticks) if err != nil { t.Error(err) } for i := 0; i < len(ticks); i++ { ti := ticks[i] t := time.Unix(ti.Timestamp/1000, 0) log.Println(ti.Timestamp/1000, t) } } func TestInstruments(t *testing.T) { req := &InstrumentsRequest{Type: "lmax"} body, err := httpreq("instruments", req) if err != nil { t.Error(err) } var inss []Instrument _, err = decodeResponse(body, &inss) if err != nil { t.Error(err) } for i, v := range inss { log.Println(i, v) } } func TestGetIns(t *testing.T) { req := &InstrumentRequest{Type: "lmax", Id: 4001} body, err := httpreq("instrument", req) if err != nil { t.Error(err) } var ins Instrument _, err = decodeResponse(body, &ins) if err != nil { t.Error(err) } log.Println(ins) } func TestStream(t *testing.T) { http2.VerboseLogs = true req := &StreamRequest{Type: "lmax"} body, err := httpreqstream("stream", req) if err != nil { t.Error(err) return } defer body.Close() decoder := json.NewDecoder(body) var tick Market for { err = decoder.Decode(&tick) if err != nil { t.Error(err) return } printDelay(DataTypeName(int(tick.Type)), fmt.Sprint(tick.InsId), tick.Timestamp) } } func TestDownload(t *testing.T) { start := time.Now().Unix() - 3600*2 req := &DownloadRequest{Type: "lmax", Start: start * 1000, End: 0, Offset: 0, Count: 1000, OrderBy: "time asc"} body, err := httpreq("download", req) if err != nil { t.Error(err) } var ticks []*Market _, err = decodeResponse(body, &ticks) if err != nil { t.Error(err) } log.Println(ticks) } func TestHistory(t *testing.T) { start := time.Now().Unix() - 10*3600 req := &DownloadRequest{Type: "lmax", Start: start * 1000, End: 0, Offset: 0, Count: 1000, OrderBy: "time asc"} body, err := httpreq("history", req) if err != nil { t.Error(err) } var ticks []TickIndex _, err = decodeResponse(body, &ticks) if err != nil { t.Error(err) } log.Println(ticks) }