-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from itsksaurabh/httptest
- use httptest package to server testfiles - update testing mechanism
- Loading branch information
Showing
13 changed files
with
112 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package wazirx_test | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
wazirx "github.com/itsksaurabh/go-wazirx" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
testServer string | ||
testDataDir = "./testdata/" | ||
updateTestData = flag.Bool("update", false, "if set then update testdata else use saved testdata for testing.") | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
|
||
// Run testServer for unit tests | ||
if !*updateTestData { | ||
server := httptest.NewServer(http.FileServer(http.Dir(testDataDir))) | ||
defer server.Close() | ||
testServer = server.URL | ||
} | ||
|
||
os.Exit(m.Run()) | ||
return | ||
} | ||
|
||
// testClient returns a wazirx.Client mainly for testing purposes. | ||
// It behaves as a reverse proxy agent, it reads testfile and | ||
// returns data as a response to requests made through it. | ||
// | ||
// If update flag is set, it will save the real data to testfile. | ||
func testClient(t *testing.T) wazirx.Client { | ||
c := wazirx.Client{ | ||
HTTP: &http.Client{}, | ||
} | ||
|
||
c.HTTP.Transport = &loaderTransport{t} | ||
|
||
if *updateTestData { | ||
c.HTTP.Transport = &saverTransport{t} | ||
} | ||
|
||
return c | ||
} | ||
|
||
// saverTransport saves response body to testdata file | ||
type saverTransport struct{ t *testing.T } | ||
|
||
func (st saverTransport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
resp, err := http.DefaultTransport.RoundTrip(r) | ||
if err != nil { | ||
return resp, errors.Wrap(err, "request failed") | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return resp, err | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return resp, errors.Wrap(err, "read body failed") | ||
} | ||
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body)) | ||
|
||
err = ioutil.WriteFile(testDataDir+filename(st.t), body, 0644) | ||
return resp, errors.Wrap(err, "write file failed") | ||
} | ||
|
||
// loaderTransport loads response from testdata file | ||
type loaderTransport struct{ t *testing.T } | ||
|
||
func (lt loaderTransport) RoundTrip(r *http.Request) (*http.Response, error) { | ||
return http.Get(testServer + "/" + filename(lt.t)) | ||
} | ||
|
||
// returns filename for saving/loading response JSON | ||
func filename(t *testing.T) string { | ||
name := t.Name() | ||
if strings.Contains(name, "/") { // If a subtest | ||
name = strings.Replace(name, "/", "_", -1) | ||
} | ||
name = strings.TrimPrefix(name, "Test") | ||
return name + ".json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"timestamp":1584182448,"asks":[["5369.0","0.1772"],["5377.0","1.1558"],["5382.0","1.1871"],["5500.0","0.7949"],["5519.0","0.1272"],["5530.0","0.0567"],["5555.0","0.1786"],["5588.0","1.46"],["5600.0","0.3385"],["5650.0","0.0212"],["5700.0","0.1609"],["5701.0","0.1"],["5730.0","0.8397"],["5749.0","0.001"],["5750.0","0.0948"],["5800.0","0.1372"]],"bids":[["5349.0","0.0261"],["5340.0","0.0088"],["5327.0","1.2933"],["5325.0","2.3665"],["5310.0","0.057"],["5290.0","0.0568"],["5230.0","0.0604"],["5223.0","0.0091"],["5205.0","0.0019"],["5200.0","0.1316"],["5119.0","0.0023"],["5111.0","0.005"],["5100.0","0.1032"],["5050.0","0.011"],["5001.0","0.6249"],["5000.0","2.177"]]} | ||
{"timestamp":1585653447,"asks":[["6376.0","1.2608"],["6377.0","1.3298"],["6381.0","0.0015"],["6383.0","1.4029"],["6453.0","0.0024"],["6480.0","0.0529"],["6484.0","0.0154"],["6487.0","0.0194"],["6489.0","0.0047"],["6490.0","0.6502"],["6494.0","0.0036"],["6495.0","0.0158"],["6500.0","0.2662"],["6501.0","0.002"],["6515.0","0.0985"],["6517.0","0.0258"]],"bids":[["6365.0","0.0273"],["6360.0","1.2919"],["6352.0","0.2329"],["6350.0","0.0245"],["6347.0","1.089"],["6342.0","2.5792"],["6341.0","1.0389"],["6335.0","0.0296"],["6316.0","0.025"],["6308.0","0.0738"],["6304.0","0.028"],["6301.0","0.0103"],["6300.0","0.5244"],["6250.0","0.0315"],["6216.0","0.0016"],["6210.0","0.1055"]]} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{"id":2601969,"price":"5368.0","volume":"0.0224","funds":"120.2432","market":"btcusdt","created_at":"2020-03-14T16:10:33+05:30","side":null},{"id":2601962,"price":"5348.0","volume":"0.0157","funds":"83.9636","market":"btcusdt","created_at":"2020-03-14T16:10:07+05:30","side":null},{"id":2601958,"price":"5358.0","volume":"0.09","funds":"482.22","market":"btcusdt","created_at":"2020-03-14T16:09:23+05:30","side":null},{"id":2601931,"price":"5369.0","volume":"0.0317","funds":"170.1973","market":"btcusdt","created_at":"2020-03-14T16:07:58+05:30","side":null},{"id":2601930,"price":"5369.0","volume":"0.0025","funds":"13.4225","market":"btcusdt","created_at":"2020-03-14T16:07:47+05:30","side":null},{"id":2601924,"price":"5350.0","volume":"0.0067","funds":"35.845","market":"btcusdt","created_at":"2020-03-14T16:07:09+05:30","side":null},{"id":2601923,"price":"5347.0","volume":"0.0095","funds":"50.7965","market":"btcusdt","created_at":"2020-03-14T16:06:56+05:30","side":null},{"id":2601914,"price":"5347.0","volume":"0.0068","funds":"36.3596","market":"btcusdt","created_at":"2020-03-14T16:06:37+05:30","side":null},{"id":2601895,"price":"5358.0","volume":"0.0111","funds":"59.4738","market":"btcusdt","created_at":"2020-03-14T16:05:22+05:30","side":null},{"id":2601874,"price":"5325.0","volume":"0.0024","funds":"12.78","market":"btcusdt","created_at":"2020-03-14T16:02:33+05:30","side":null},{"id":2601858,"price":"5350.0","volume":"0.0366","funds":"195.81","market":"btcusdt","created_at":"2020-03-14T16:01:04+05:30","side":null},{"id":2601857,"price":"5322.0","volume":"0.2442","funds":"1299.6324","market":"btcusdt","created_at":"2020-03-14T16:01:04+05:30","side":null},{"id":2601852,"price":"5322.0","volume":"0.0157","funds":"83.5554","market":"btcusdt","created_at":"2020-03-14T16:00:50+05:30","side":null},{"id":2601843,"price":"5309.0","volume":"0.0014","funds":"7.4326","market":"btcusdt","created_at":"2020-03-14T16:00:40+05:30","side":null},{"id":2601830,"price":"5300.0","volume":"0.2497","funds":"1323.41","market":"btcusdt","created_at":"2020-03-14T16:00:03+05:30","side":null},{"id":2601829,"price":"5300.0","volume":"0.0834","funds":"442.02","market":"btcusdt","created_at":"2020-03-14T16:00:03+05:30","side":null},{"id":2601817,"price":"5300.0","volume":"0.0094","funds":"49.82","market":"btcusdt","created_at":"2020-03-14T15:59:48+05:30","side":null},{"id":2601792,"price":"5300.0","volume":"0.0933","funds":"494.49","market":"btcusdt","created_at":"2020-03-14T15:58:01+05:30","side":null},{"id":2601789,"price":"5278.0","volume":"0.0022","funds":"11.6116","market":"btcusdt","created_at":"2020-03-14T15:57:55+05:30","side":null},{"id":2601766,"price":"5266.0","volume":"0.0035","funds":"18.431","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601765,"price":"5266.0","volume":"0.0182","funds":"95.8412","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601764,"price":"5265.0","volume":"0.0819","funds":"431.2035","market":"btcusdt","created_at":"2020-03-14T15:56:58+05:30","side":null},{"id":2601760,"price":"5265.0","volume":"0.0182","funds":"95.823","market":"btcusdt","created_at":"2020-03-14T15:56:17+05:30","side":null},{"id":2601734,"price":"5242.0","volume":"0.016","funds":"83.872","market":"btcusdt","created_at":"2020-03-14T15:55:05+05:30","side":null},{"id":2601704,"price":"5266.0","volume":"0.0177","funds":"93.2082","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601703,"price":"5266.0","volume":"0.0048","funds":"25.2768","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601702,"price":"5248.0","volume":"0.041","funds":"215.168","market":"btcusdt","created_at":"2020-03-14T15:53:01+05:30","side":null},{"id":2601698,"price":"5248.0","volume":"0.0261","funds":"136.9728","market":"btcusdt","created_at":"2020-03-14T15:52:55+05:30","side":null},{"id":2601662,"price":"5241.0","volume":"0.074","funds":"387.834","market":"btcusdt","created_at":"2020-03-14T15:50:49+05:30","side":null},{"id":2601601,"price":"5243.0","volume":"0.0022","funds":"11.5346","market":"btcusdt","created_at":"2020-03-14T15:46:56+05:30","side":null},{"id":2601590,"price":"5243.0","volume":"0.0022","funds":"11.5346","market":"btcusdt","created_at":"2020-03-14T15:46:19+05:30","side":null},{"id":2601585,"price":"5243.0","volume":"0.0095","funds":"49.8085","market":"btcusdt","created_at":"2020-03-14T15:46:08+05:30","side":null},{"id":2601555,"price":"5243.0","volume":"0.0389","funds":"203.9527","market":"btcusdt","created_at":"2020-03-14T15:43:29+05:30","side":null},{"id":2601488,"price":"5223.0","volume":"0.0092","funds":"48.0516","market":"btcusdt","created_at":"2020-03-14T15:38:02+05:30","side":null},{"id":2601478,"price":"5230.0","volume":"0.4126","funds":"2157.898","market":"btcusdt","created_at":"2020-03-14T15:37:32+05:30","side":null},{"id":2601460,"price":"5222.0","volume":"1.1163","funds":"5829.3186","market":"btcusdt","created_at":"2020-03-14T15:36:18+05:30","side":null},{"id":2601436,"price":"5245.0","volume":"0.0012","funds":"6.294","market":"btcusdt","created_at":"2020-03-14T15:35:13+05:30","side":null},{"id":2601433,"price":"5216.0","volume":"0.02","funds":"104.32","market":"btcusdt","created_at":"2020-03-14T15:35:08+05:30","side":null},{"id":2601412,"price":"5232.0","volume":"0.1","funds":"523.2","market":"btcusdt","created_at":"2020-03-14T15:34:08+05:30","side":null},{"id":2601407,"price":"5260.0","volume":"0.0302","funds":"158.852","market":"btcusdt","created_at":"2020-03-14T15:33:54+05:30","side":null},{"id":2601389,"price":"5231.0","volume":"0.097","funds":"507.407","market":"btcusdt","created_at":"2020-03-14T15:33:08+05:30","side":null},{"id":2601388,"price":"5244.0","volume":"0.003","funds":"15.732","market":"btcusdt","created_at":"2020-03-14T15:33:08+05:30","side":null},{"id":2601364,"price":"5254.0","volume":"0.0763","funds":"400.8802","market":"btcusdt","created_at":"2020-03-14T15:31:58+05:30","side":null},{"id":2601363,"price":"5245.0","volume":"0.0012","funds":"6.294","market":"btcusdt","created_at":"2020-03-14T15:31:58+05:30","side":null},{"id":2601352,"price":"5230.0","volume":"0.2714","funds":"1419.422","market":"btcusdt","created_at":"2020-03-14T15:31:08+05:30","side":null},{"id":2601349,"price":"5245.0","volume":"0.0041","funds":"21.5045","market":"btcusdt","created_at":"2020-03-14T15:30:48+05:30","side":null},{"id":2601347,"price":"5215.0","volume":"0.001","funds":"5.215","market":"btcusdt","created_at":"2020-03-14T15:30:38+05:30","side":null},{"id":2601346,"price":"5214.0","volume":"0.0431","funds":"224.7234","market":"btcusdt","created_at":"2020-03-14T15:30:38+05:30","side":null},{"id":2601337,"price":"5213.0","volume":"0.0041","funds":"21.3733","market":"btcusdt","created_at":"2020-03-14T15:30:12+05:30","side":null},{"id":2601319,"price":"5214.0","volume":"0.0058","funds":"30.2412","market":"btcusdt","created_at":"2020-03-14T15:29:22+05:30","side":null}] | ||
[{"id":2944138,"price":"6367.0","volume":"0.0464","funds":"295.4288","market":"btcusdt","created_at":"2020-03-31T16:46:39+05:30","side":null},{"id":2944135,"price":"6369.0","volume":"0.0984","funds":"626.7096","market":"btcusdt","created_at":"2020-03-31T16:46:15+05:30","side":null},{"id":2944132,"price":"6369.0","volume":"0.0363","funds":"231.1947","market":"btcusdt","created_at":"2020-03-31T16:46:02+05:30","side":null},{"id":2944131,"price":"6371.0","volume":"0.0028","funds":"17.8388","market":"btcusdt","created_at":"2020-03-31T16:45:56+05:30","side":null},{"id":2944128,"price":"6381.0","volume":"0.001","funds":"6.381","market":"btcusdt","created_at":"2020-03-31T16:45:45+05:30","side":null},{"id":2944126,"price":"6381.0","volume":"0.0015","funds":"9.5715","market":"btcusdt","created_at":"2020-03-31T16:45:33+05:30","side":null},{"id":2944116,"price":"6383.0","volume":"0.0017","funds":"10.8511","market":"btcusdt","created_at":"2020-03-31T16:44:23+05:30","side":null},{"id":2944107,"price":"6355.0","volume":"0.0563","funds":"357.7865","market":"btcusdt","created_at":"2020-03-31T16:43:59+05:30","side":null},{"id":2944091,"price":"6379.0","volume":"0.2439","funds":"1555.8381","market":"btcusdt","created_at":"2020-03-31T16:43:23+05:30","side":null},{"id":2944090,"price":"6385.0","volume":"0.0079","funds":"50.4415","market":"btcusdt","created_at":"2020-03-31T16:43:22+05:30","side":null},{"id":2944089,"price":"6386.0","volume":"0.0587","funds":"374.8582","market":"btcusdt","created_at":"2020-03-31T16:43:22+05:30","side":null},{"id":2944088,"price":"6386.0","volume":"0.9856","funds":"6294.0416","market":"btcusdt","created_at":"2020-03-31T16:43:21+05:30","side":null},{"id":2944074,"price":"6385.0","volume":"0.436","funds":"2783.86","market":"btcusdt","created_at":"2020-03-31T16:42:59+05:30","side":null},{"id":2944058,"price":"6380.0","volume":"0.1622","funds":"1034.836","market":"btcusdt","created_at":"2020-03-31T16:42:13+05:30","side":null},{"id":2944057,"price":"6380.0","volume":"0.0104","funds":"66.352","market":"btcusdt","created_at":"2020-03-31T16:42:13+05:30","side":null},{"id":2944039,"price":"6370.0","volume":"0.0121","funds":"77.077","market":"btcusdt","created_at":"2020-03-31T16:41:45+05:30","side":null},{"id":2944018,"price":"6369.0","volume":"0.2","funds":"1273.8","market":"btcusdt","created_at":"2020-03-31T16:41:24+05:30","side":null},{"id":2944014,"price":"6369.0","volume":"0.2174","funds":"1384.6206","market":"btcusdt","created_at":"2020-03-31T16:41:21+05:30","side":null},{"id":2944007,"price":"6379.0","volume":"0.591","funds":"3769.989","market":"btcusdt","created_at":"2020-03-31T16:41:14+05:30","side":null},{"id":2943992,"price":"6379.0","volume":"0.3943","funds":"2515.2397","market":"btcusdt","created_at":"2020-03-31T16:41:07+05:30","side":null},{"id":2943966,"price":"6400.0","volume":"0.01","funds":"64.0","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943965,"price":"6400.0","volume":"0.0512","funds":"327.68","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943964,"price":"6400.0","volume":"0.0326","funds":"208.64","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943963,"price":"6400.0","volume":"0.0066","funds":"42.24","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943962,"price":"6402.0","volume":"0.0018","funds":"11.5236","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943961,"price":"6403.0","volume":"0.002","funds":"12.806","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943960,"price":"6404.0","volume":"0.8242","funds":"5278.1768","market":"btcusdt","created_at":"2020-03-31T16:40:37+05:30","side":null},{"id":2943958,"price":"6404.0","volume":"0.0405","funds":"259.362","market":"btcusdt","created_at":"2020-03-31T16:40:33+05:30","side":null},{"id":2943950,"price":"6404.0","volume":"0.297","funds":"1901.988","market":"btcusdt","created_at":"2020-03-31T16:40:28+05:30","side":null},{"id":2943944,"price":"6404.0","volume":"1.0887","funds":"6972.0348","market":"btcusdt","created_at":"2020-03-31T16:40:27+05:30","side":null},{"id":2943943,"price":"6405.0","volume":"0.0215","funds":"137.7075","market":"btcusdt","created_at":"2020-03-31T16:40:27+05:30","side":null},{"id":2943942,"price":"6410.0","volume":"0.0196","funds":"125.636","market":"btcusdt","created_at":"2020-03-31T16:40:27+05:30","side":null},{"id":2943941,"price":"6410.0","volume":"0.0377","funds":"241.657","market":"btcusdt","created_at":"2020-03-31T16:40:27+05:30","side":null},{"id":2943938,"price":"6416.0","volume":"0.894","funds":"5735.904","market":"btcusdt","created_at":"2020-03-31T16:40:21+05:30","side":null},{"id":2943934,"price":"6426.0","volume":"0.001","funds":"6.426","market":"btcusdt","created_at":"2020-03-31T16:40:15+05:30","side":null},{"id":2943933,"price":"6416.0","volume":"0.2925","funds":"1876.68","market":"btcusdt","created_at":"2020-03-31T16:40:14+05:30","side":null},{"id":2943932,"price":"6421.0","volume":"0.026","funds":"166.946","market":"btcusdt","created_at":"2020-03-31T16:40:13+05:30","side":null},{"id":2943923,"price":"6432.0","volume":"0.0067","funds":"43.0944","market":"btcusdt","created_at":"2020-03-31T16:39:27+05:30","side":null},{"id":2943913,"price":"6433.0","volume":"0.765","funds":"4921.245","market":"btcusdt","created_at":"2020-03-31T16:37:57+05:30","side":null},{"id":2943911,"price":"6438.0","volume":"0.0089","funds":"57.2982","market":"btcusdt","created_at":"2020-03-31T16:37:47+05:30","side":null},{"id":2943909,"price":"6438.0","volume":"1.3876","funds":"8933.3688","market":"btcusdt","created_at":"2020-03-31T16:37:45+05:30","side":null},{"id":2943908,"price":"6434.0","volume":"0.0089","funds":"57.2626","market":"btcusdt","created_at":"2020-03-31T16:37:40+05:30","side":null},{"id":2943905,"price":"6433.0","volume":"0.0024","funds":"15.4392","market":"btcusdt","created_at":"2020-03-31T16:37:38+05:30","side":null},{"id":2943886,"price":"6421.0","volume":"0.004","funds":"25.684","market":"btcusdt","created_at":"2020-03-31T16:35:29+05:30","side":null},{"id":2943856,"price":"6432.0","volume":"0.002","funds":"12.864","market":"btcusdt","created_at":"2020-03-31T16:30:38+05:30","side":null},{"id":2943827,"price":"6416.0","volume":"0.0024","funds":"15.3984","market":"btcusdt","created_at":"2020-03-31T16:28:00+05:30","side":null},{"id":2943793,"price":"6423.0","volume":"0.0114","funds":"73.2222","market":"btcusdt","created_at":"2020-03-31T16:25:43+05:30","side":null},{"id":2943786,"price":"6427.0","volume":"0.011","funds":"70.697","market":"btcusdt","created_at":"2020-03-31T16:24:47+05:30","side":null},{"id":2943784,"price":"6427.0","volume":"0.0064","funds":"41.1328","market":"btcusdt","created_at":"2020-03-31T16:24:45+05:30","side":null},{"id":2943773,"price":"6427.0","volume":"0.002","funds":"12.854","market":"btcusdt","created_at":"2020-03-31T16:24:06+05:30","side":null}] |
Oops, something went wrong.