Fixed Linter issues & code formatting

This commit is contained in:
Ryan O'Hara-Reid
2017-07-23 15:57:56 +10:00
committed by Adrian Gallagher
parent 5059564d4a
commit 06786ed9de
2 changed files with 195 additions and 55 deletions

View File

@@ -26,7 +26,7 @@ import (
"time"
)
// const declarations
// Const declarations for common.go operations
const (
HashSHA1 = iota
HashSHA256
@@ -37,24 +37,29 @@ const (
WeiPerEther = 1000000000000000000
)
// GetMD5 returns a MD5 hash of a byte array
func GetMD5(input []byte) []byte {
hash := md5.New()
hash.Write(input)
return hash.Sum(nil)
}
// GetSHA512 returns a SHA512 hash of a byte array
func GetSHA512(input []byte) []byte {
sha := sha512.New()
sha.Write(input)
return sha.Sum(nil)
}
// GetSHA256 returns a SHA256 hash of a byte array
func GetSHA256(input []byte) []byte {
sha := sha256.New()
sha.Write(input)
return sha.Sum(nil)
}
// GetHMAC returns a keyed-hash message authentication code using the desired
// hashtype
func GetHMAC(hashType int, input, key []byte) []byte {
var hash func() hash.Hash
@@ -82,10 +87,12 @@ func GetHMAC(hashType int, input, key []byte) []byte {
return hmac.Sum(nil)
}
// HexEncodeToString takes in a hexidecimal byte array and returns a string
func HexEncodeToString(input []byte) string {
return hex.EncodeToString(input)
}
// Base64Decode takes in a Base64 string and returns a byte array and an error
func Base64Decode(input string) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(input)
if err != nil {
@@ -94,10 +101,13 @@ func Base64Decode(input string) ([]byte, error) {
return result, nil
}
// Base64Encode takes in a byte array then returns an encoded base64 string
func Base64Encode(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
// StringSliceDifference concatenates slices together based on its index and
// returns an individual string array
func StringSliceDifference(slice1 []string, slice2 []string) []string {
var diff []string
for i := 0; i < 2; i++ {
@@ -120,35 +130,46 @@ func StringSliceDifference(slice1 []string, slice2 []string) []string {
return diff
}
// StringContains checks a substring if it contains your input then returns a
// bool
func StringContains(input, substring string) bool {
return strings.Contains(input, substring)
}
// DataContains checks the substring array with an input and returns a bool
func DataContains(haystack []string, needle string) bool {
data := strings.Join(haystack, ",")
return strings.Contains(data, needle)
}
// JoinStrings joins an array together with the required seperator and returns
// it as a string
func JoinStrings(input []string, seperator string) string {
return strings.Join(input, seperator)
}
// SplitStrings splits blocks of strings from string into a string array using
// a seperator ie "," or "_"
func SplitStrings(input, seperator string) []string {
return strings.Split(input, seperator)
}
// TrimString trims unwanted prefixes or postfixes
func TrimString(input, cutset string) string {
return strings.Trim(input, cutset)
}
// StringToUpper changes strings to uppercase
func StringToUpper(input string) string {
return strings.ToUpper(input)
}
// StringToLower changes strings to lowercase
func StringToLower(input string) string {
return strings.ToLower(input)
}
// RoundFloat rounds your floating point number to the desired decimal place
func RoundFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
@@ -158,7 +179,7 @@ func RoundFloat(x float64, prec int) float64 {
x = .5
if frac < 0.0 {
x = -.5
intermed -= 1
intermed--
}
if frac >= x {
rounder = math.Ceil(intermed)
@@ -169,6 +190,8 @@ func RoundFloat(x float64, prec int) float64 {
return rounder / pow
}
// IsEnabled takes in a boolean param and returns a string if it is enabled
// or disabled
func IsEnabled(isEnabled bool) string {
if isEnabled {
return "Enabled"
@@ -176,7 +199,8 @@ func IsEnabled(isEnabled bool) string {
return "Disabled"
}
// IsValidCryptoAddress validates your cryptocurrency address string using the regexp package
// IsValidCryptoAddress validates your cryptocurrency address string using the
// regexp package
func IsValidCryptoAddress(address, crypto string) (bool, error) {
switch StringToLower(crypto) {
case "btc":
@@ -190,6 +214,7 @@ func IsValidCryptoAddress(address, crypto string) (bool, error) {
}
}
// YesOrNo returns a boolean variable to check if input is "y" or "yes"
func YesOrNo(input string) bool {
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
return true
@@ -197,31 +222,40 @@ func YesOrNo(input string) bool {
return false
}
// CalculateAmountWithFee returns a calculated fee included amount on fee
func CalculateAmountWithFee(amount, fee float64) float64 {
return amount + CalculateFee(amount, fee)
}
// CalculateFee retuns a simple fee on amount
func CalculateFee(amount, fee float64) float64 {
return amount * (fee / 100)
}
// CalculatePercentageGainOrLoss returns the percentage rise over a certain
// period
func CalculatePercentageGainOrLoss(priceNow, priceThen float64) float64 {
return (priceNow - priceThen) / priceThen * 100
}
// CalculatePercentageDifference returns the percentage of difference between
// multiple time periods
func CalculatePercentageDifference(amount, secondAmount float64) float64 {
return (amount - secondAmount) / ((amount + secondAmount) / 2) * 100
}
// CalculateNetProfit returns net profit
func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64 {
return (priceNow * amount) - (priceThen * amount) - costs
}
// SendHTTPRequest sends a request using the http package and returns a response
// as a string and an error
func SendHTTPRequest(method, path string, headers map[string]string, body io.Reader) (string, error) {
result := strings.ToUpper(method)
if result != "POST" && result != "GET" && result != "DELETE" {
return "", errors.New("Invalid HTTP method specified.")
return "", errors.New("invalid HTTP method specified")
}
req, err := http.NewRequest(method, path, body)
@@ -251,6 +285,9 @@ func SendHTTPRequest(method, path string, headers map[string]string, body io.Rea
return string(contents), nil
}
// SendHTTPGetRequest sends a simple get request using a url string & JSON
// decodes the response into a struct pointer you have supplied. Returns an error
// on failure.
func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) error {
res, err := http.Get(url)
if err != nil {
@@ -259,7 +296,7 @@ func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) error {
if res.StatusCode != 200 {
log.Printf("HTTP status code: %d\n", res.StatusCode)
return errors.New("Status code was not 200.")
return errors.New("status code was not 200")
}
contents, err := ioutil.ReadAll(res.Body)
@@ -282,14 +319,18 @@ func SendHTTPGetRequest(url string, jsonDecode bool, result interface{}) error {
return nil
}
// JSONEncode encodes structure data into JSON
func JSONEncode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// JSONDecode decodes JSON data into a structure
func JSONDecode(data []byte, to interface{}) error {
return json.Unmarshal(data, to)
}
// EncodeURLValues concatenates url values onto a url string and returns a
// string
func EncodeURLValues(url string, values url.Values) string {
path := url
if len(values) > 0 {
@@ -298,6 +339,7 @@ func EncodeURLValues(url string, values url.Values) string {
return path
}
// ExtractHost returns the hostname out of a string
func ExtractHost(address string) string {
host := SplitStrings(address, ":")[0]
if host == "" {
@@ -306,12 +348,14 @@ func ExtractHost(address string) string {
return host
}
// ExtractPort returns the port name out of a string
func ExtractPort(host string) int {
portStr := SplitStrings(host, ":")[1]
port, _ := strconv.Atoi(portStr)
return port
}
// OutputCSV dumps data into a file as comma-seperated values
func OutputCSV(path string, data [][]string) error {
_, err := ReadFile(path)
if err != nil {
@@ -337,10 +381,12 @@ func OutputCSV(path string, data [][]string) error {
return nil
}
// UnixTimestampToTime returns time.time
func UnixTimestampToTime(timeint64 int64) time.Time {
return time.Unix(timeint64, 0)
}
// UnixTimestampStrToTime returns a time.time and an error
func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
i, err := strconv.ParseInt(timeStr, 10, 64)
if err != nil {
@@ -350,6 +396,7 @@ func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
return time.Unix(i, 0), nil
}
// ReadFile reads a file and returns read data as byte array.
func ReadFile(path string) ([]byte, error) {
file, err := ioutil.ReadFile(path)
if err != nil {
@@ -358,6 +405,7 @@ func ReadFile(path string) ([]byte, error) {
return file, nil
}
// WriteFile writes selected data to a file and returns an error
func WriteFile(file string, data []byte) error {
err := ioutil.WriteFile(file, data, 0644)
if err != nil {

View File

@@ -15,13 +15,17 @@ func TestIsEnabled(t *testing.T) {
expected := "Enabled"
actual := IsEnabled(true)
if actual != expected {
t.Error(fmt.Sprintf("Test failed. Expected %s. Actual %s", expected, actual))
t.Error(fmt.Sprintf(
"Test failed. Expected %s. Actual %s", expected, actual),
)
}
expected = "Disabled"
actual = IsEnabled(false)
if actual != expected {
t.Error(fmt.Sprintf("Test failed. Expected %s. Actual %s", expected, actual))
t.Error(fmt.Sprintf(
"Test failed. Expected %s. Actual %s", expected, actual),
)
}
}
@@ -48,15 +52,24 @@ func TestIsValidCryptoAddress(t *testing.T) {
if err == nil && b {
t.Error("Test Failed - Common IsValidCryptoAddress error")
}
b, err = IsValidCryptoAddress("0xb794f5ea0ba39494ce839613fffba74279579268", "eth")
b, err = IsValidCryptoAddress(
"0xb794f5ea0ba39494ce839613fffba74279579268",
"eth",
)
if err != nil && b {
t.Errorf("Test Failed - Common IsValidCryptoAddress error: %s", err)
}
b, err = IsValidCryptoAddress("xxb794f5ea0ba39494ce839613fffba74279579268", "eTh")
b, err = IsValidCryptoAddress(
"xxb794f5ea0ba39494ce839613fffba74279579268",
"eTh",
)
if err == nil && b {
t.Error("Test Failed - Common IsValidCryptoAddress error")
}
b, err = IsValidCryptoAddress("xxb794f5ea0ba39494ce839613fffba74279579268", "ding")
b, err = IsValidCryptoAddress(
"xxb794f5ea0ba39494ce839613fffba74279579268",
"ding",
)
if err == nil && b {
t.Error("Test Failed - Common IsValidCryptoAddress error")
}
@@ -69,7 +82,10 @@ func TestGetMD5(t *testing.T) {
actualOutput := GetMD5(originalString)
actualStr := HexEncodeToString(actualOutput)
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, []byte(actualStr)))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'",
expectedOutput, []byte(actualStr)),
)
}
}
@@ -77,22 +93,30 @@ func TestGetMD5(t *testing.T) {
func TestGetSHA512(t *testing.T) {
t.Parallel()
var originalString = []byte("I am testing the GetSHA512 function in common!")
var expectedOutput = []byte("a2273f492ea73fddc4f25c267b34b3b74998bd8a6301149e1e1c835678e3c0b90859fce22e4e7af33bde1711cbb924809aedf5d759d648d61774b7185c5dc02b")
var expectedOutput = []byte(
`a2273f492ea73fddc4f25c267b34b3b74998bd8a6301149e1e1c835678e3c0b90859fce22e4e7af33bde1711cbb924809aedf5d759d648d61774b7185c5dc02b`,
)
actualOutput := GetSHA512(originalString)
actualStr := HexEncodeToString(actualOutput)
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
t.Error(fmt.Sprintf("Test failed. Expected '%x'. Actual '%x'", expectedOutput, []byte(actualStr)))
t.Error(fmt.Sprintf("Test failed. Expected '%x'. Actual '%x'",
expectedOutput, []byte(actualStr)),
)
}
}
func TestGetSHA256(t *testing.T) {
t.Parallel()
var originalString = []byte("I am testing the GetSHA256 function in common!")
var expectedOutput = []byte("0962813d7a9f739cdcb7f0c0be0c2a13bd630167e6e54468266e4af6b1ad9303")
var expectedOutput = []byte(
"0962813d7a9f739cdcb7f0c0be0c2a13bd630167e6e54468266e4af6b1ad9303",
)
actualOutput := GetSHA256(originalString)
actualStr := HexEncodeToString(actualOutput)
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
t.Error(fmt.Sprintf("Test failed. Expected '%x'. Actual '%x'", expectedOutput, []byte(actualStr)))
t.Error(fmt.Sprintf("Test failed. Expected '%x'. Actual '%x'",
expectedOutput, []byte(actualStr)),
)
}
}
@@ -102,35 +126,44 @@ func TestGetHMAC(t *testing.T) {
100, 66, 86, 246,
}
expectedsha256 := []byte{
54, 68, 6, 12, 32, 158, 80, 22, 142, 8, 131, 111, 248, 145, 17, 202, 224, 59, 135, 206, 11, 170,
154, 197, 183, 28, 150, 79, 168, 105, 62, 102,
54, 68, 6, 12, 32, 158, 80, 22, 142, 8, 131, 111, 248, 145, 17, 202, 224,
59, 135, 206, 11, 170, 154, 197, 183, 28, 150, 79, 168, 105, 62, 102,
}
expectedsha512 := []byte{
249, 212, 31, 38, 23, 3, 93, 220, 81, 209, 214, 112, 92, 75, 126, 40, 109, 95, 247, 182, 210, 54,
217, 224, 199, 252, 129, 226, 97, 201, 245, 220, 37, 201, 240, 15, 137, 236, 75, 6, 97, 12, 190,
31, 53, 153, 223, 17, 214, 11, 153, 203, 49, 29, 158, 217, 204, 93, 179, 109, 140, 216, 202, 71,
249, 212, 31, 38, 23, 3, 93, 220, 81, 209, 214, 112, 92, 75, 126, 40, 109,
95, 247, 182, 210, 54, 217, 224, 199, 252, 129, 226, 97, 201, 245, 220, 37,
201, 240, 15, 137, 236, 75, 6, 97, 12, 190, 31, 53, 153, 223, 17, 214, 11,
153, 203, 49, 29, 158, 217, 204, 93, 179, 109, 140, 216, 202, 71,
}
expectedsha512384 := []byte{
121, 203, 109, 105, 178, 68, 179, 57, 21, 217, 76, 82, 94, 100, 210, 1, 55, 201, 8, 232, 194,
168, 165, 58, 192, 26, 193, 167, 254, 183, 172, 4, 189, 158, 158, 150, 173, 33, 119, 125, 94,
13, 125, 89, 241, 184, 166, 128,
121, 203, 109, 105, 178, 68, 179, 57, 21, 217, 76, 82, 94, 100, 210, 1, 55,
201, 8, 232, 194, 168, 165, 58, 192, 26, 193, 167, 254, 183, 172, 4, 189,
158, 158, 150, 173, 33, 119, 125, 94, 13, 125, 89, 241, 184, 166, 128,
}
sha1 := GetHMAC(HashSHA1, []byte("Hello,World"), []byte("1234"))
if string(sha1) != string(expectedSha1) {
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'", expectedSha1, sha1)
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
sha256 := GetHMAC(HashSHA256, []byte("Hello,World"), []byte("1234"))
if string(sha256) != string(expectedsha256) {
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'", expectedSha1, sha1)
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
sha512 := GetHMAC(HashSHA512, []byte("Hello,World"), []byte("1234"))
if string(sha512) != string(expectedsha512) {
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'", expectedSha1, sha1)
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
sha512384 := GetHMAC(HashSHA512_384, []byte("Hello,World"), []byte("1234"))
if string(sha512384) != string(expectedsha512384) {
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'", expectedSha1, sha1)
t.Errorf("Test failed.Common GetHMAC error: Expected '%x'. Actual '%x'",
expectedSha1, sha1,
)
}
}
@@ -160,7 +193,9 @@ func TestHexEncodeToString(t *testing.T) {
expectedOutput := "737472696e67"
actualResult := HexEncodeToString(originalInput)
if actualResult != expectedOutput {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult))
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'",
expectedOutput, actualResult),
)
}
}
@@ -170,7 +205,9 @@ func TestBase64Decode(t *testing.T) {
expectedOutput := []byte("hello")
actualResult, err := Base64Decode(originalInput)
if !bytes.Equal(actualResult, expectedOutput) {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'. Error: %s", expectedOutput, actualResult, err))
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'. Error: %s",
expectedOutput, actualResult, err),
)
}
}
@@ -180,7 +217,9 @@ func TestBase64Encode(t *testing.T) {
expectedOutput := "aGVsbG8="
actualResult := Base64Encode(originalInput)
if actualResult != expectedOutput {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult),
)
}
}
@@ -191,7 +230,9 @@ func TestStringSliceDifference(t *testing.T) {
expectedOutput := []string{"hello moto"}
actualResult := StringSliceDifference(originalInputOne, originalInputTwo)
if reflect.DeepEqual(expectedOutput, actualResult) {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult),
)
}
}
@@ -202,7 +243,9 @@ func TestStringContains(t *testing.T) {
expectedOutput := true
actualResult := StringContains(originalInput, originalInputSubstring)
if actualResult != expectedOutput {
t.Error(fmt.Sprintf("Test failed. Expected '%t'. Actual '%t'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%t'. Actual '%t'", expectedOutput, actualResult),
)
}
}
@@ -215,11 +258,16 @@ func TestDataContains(t *testing.T) {
expectedOutputTwo := false
actualResult := DataContains(originalHaystack, originalNeedle)
if actualResult != expectedOutput {
t.Error(fmt.Sprintf("Test failed. Expected '%t'. Actual '%t'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%t'. Actual '%t'", expectedOutput, actualResult),
)
}
actualResult = DataContains(originalHaystack, anotherNeedle)
if actualResult != expectedOutputTwo {
t.Error(fmt.Sprintf("Test failed. Expected '%t'. Actual '%t'", expectedOutputTwo, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%t'. Actual '%t'", expectedOutputTwo,
actualResult),
)
}
}
@@ -230,7 +278,9 @@ func TestJoinStrings(t *testing.T) {
expectedOutput := "hello,moto"
actualResult := JoinStrings(originalInputOne, seperator)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult),
)
}
}
@@ -241,7 +291,9 @@ func TestSplitStrings(t *testing.T) {
expectedOutput := []string{"hello", "moto"}
actualResult := SplitStrings(originalInputOne, seperator)
if !reflect.DeepEqual(expectedOutput, actualResult) {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult),
)
}
}
@@ -252,7 +304,9 @@ func TestTrimString(t *testing.T) {
expectedOutput := "bc"
actualResult := TrimString(originalInput, cutset)
if expectedOutput != actualResult {
t.Errorf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult)
t.Errorf(
"Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult,
)
}
}
@@ -263,7 +317,9 @@ func TestRoundFloat(t *testing.T) {
expectedOutput := float64(1.45)
actualResult := RoundFloat(originalInput, precisionInput)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -287,7 +343,9 @@ func TestCalculateFee(t *testing.T) {
expectedOutput := float64(0.01)
actualResult := CalculateFee(originalInput, fee)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -298,7 +356,9 @@ func TestCalculateAmountWithFee(t *testing.T) {
expectedOutput := float64(1.01)
actualResult := CalculateAmountWithFee(originalInput, fee)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -309,7 +369,9 @@ func TestCalculatePercentageGainOrLoss(t *testing.T) {
expectedOutput := 3.3333333333333335
actualResult := CalculatePercentageGainOrLoss(originalInput, secondInput)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -320,7 +382,9 @@ func TestCalculatePercentageDifference(t *testing.T) {
expectedOutput := 66.66666666666666
actualResult := CalculatePercentageDifference(originalInput, secondAmount)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -333,7 +397,9 @@ func TestCalculateNetProfit(t *testing.T) {
expectedOutput := float64(44)
actualResult := CalculateNetProfit(amount, priceThen, priceNow, costs)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%f'. Actual '%f'.", expectedOutput, actualResult),
)
}
}
@@ -346,19 +412,31 @@ func TestSendHTTPRequest(t *testing.T) {
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
_, err := SendHTTPRequest(methodGarbage, "http://query.yahooapis.com/v1/public/yql", headers, strings.NewReader(""))
_, err := SendHTTPRequest(
methodGarbage, "http://query.yahooapis.com/v1/public/yql", headers,
strings.NewReader(""),
)
if err == nil {
t.Error("Test failed. ")
}
_, err = SendHTTPRequest(methodPost, "http://query.yahooapis.com/v1/public/yql", headers, strings.NewReader(""))
_, err = SendHTTPRequest(
methodPost, "http://query.yahooapis.com/v1/public/yql", headers,
strings.NewReader(""),
)
if err != nil {
t.Errorf("Test failed. %s ", err)
}
_, err = SendHTTPRequest(methodGet, "http://query.yahooapis.com/v1/public/yql", headers, strings.NewReader(""))
_, err = SendHTTPRequest(
methodGet, "http://query.yahooapis.com/v1/public/yql", headers,
strings.NewReader(""),
)
if err != nil {
t.Errorf("Test failed. %s ", err)
}
_, err = SendHTTPRequest(methodDelete, "http://query.yahooapis.com/v1/public/yql", headers, strings.NewReader(""))
_, err = SendHTTPRequest(
methodDelete, "http://query.yahooapis.com/v1/public/yql", headers,
strings.NewReader(""),
)
if err != nil {
t.Errorf("Test failed. %s ", err)
}
@@ -377,7 +455,7 @@ func TestSendHTTPGetRequest(t *testing.T) {
FirstSeen interface{} `json:"firstSeen"`
} `json:"data"`
}
url := "https://etherchain.org/api/account/multiple/0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe"
url := `https://etherchain.org/api/account/multiple/0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe`
result := test{}
err := SendHTTPGetRequest(url, true, &result)
@@ -427,7 +505,9 @@ func TestEncodeURLValues(t *testing.T) {
urlstring := "http://www.test.com"
expectedOutput := `http://www.test.com?env=TEST%2FDATABASE&format=json&q=SELECT+%2A+from+yahoo.finance.xchange+WHERE+pair+in+%28%22BTC%2CUSD%22%29`
values := url.Values{}
values.Set("q", fmt.Sprintf("SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", "BTC,USD"))
values.Set("q", fmt.Sprintf(
"SELECT * from yahoo.finance.xchange WHERE pair in (\"%s\")", "BTC,USD"),
)
values.Set("format", "json")
values.Set("env", "TEST/DATABASE")
@@ -444,18 +524,24 @@ func TestExtractHost(t *testing.T) {
expectedOutput := "localhost"
actualResult := ExtractHost(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult),
)
}
actualResultTwo := ExtractHost(addresstwo)
if expectedOutput != actualResultTwo {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult),
)
}
address = "192.168.1.100:1337"
expectedOutput = "192.168.1.100"
actualResult = ExtractHost(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult),
)
}
}
@@ -465,7 +551,9 @@ func TestExtractPort(t *testing.T) {
expectedOutput := 1337
actualResult := ExtractPort(address)
if expectedOutput != actualResult {
t.Error(fmt.Sprintf("Test failed. Expected '%d'. Actual '%d'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%d'. Actual '%d'.", expectedOutput, actualResult),
)
}
}
@@ -491,7 +579,9 @@ func TestUnixTimestampToTime(t *testing.T) {
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult := UnixTimestampToTime(testTime)
if tm.String() != actualResult.String() {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult),
)
}
}
@@ -505,7 +595,9 @@ func TestUnixTimestampStrToTime(t *testing.T) {
t.Error(err)
}
if actualResult.UTC().String() != expectedOutput {
t.Error(fmt.Sprintf("Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult))
t.Error(fmt.Sprintf(
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult),
)
}
actualResult, err = UnixTimestampStrToTime(incorrectTime)
if err == nil {