90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package slownie
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type slownieResponse struct {
|
|
Error int
|
|
Data slownieResponseData `json:"data"`
|
|
}
|
|
|
|
type slownieResponseData struct {
|
|
Word string
|
|
Header string
|
|
}
|
|
|
|
var client = &http.Client{}
|
|
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
var space = regexp.MustCompile(`\s+`)
|
|
var cookie1 = &http.Cookie{Name: "PHPSESSID", Value: "shplbjsjd3o4fdt9iutbb0o233", HttpOnly: false}
|
|
|
|
func singleTest(t *testing.T, x float64) {
|
|
if kwota, err := KwotaSlownie(x, false); err == nil {
|
|
valueAsString := fmt.Sprintf("%.2f", x)
|
|
|
|
reqBody := url.Values{"number": {valueAsString}, "currency": {"pln"}, "country": {"pl"}, "csrf": {"fc64dfc02c3ba52eddea990c836e7e8d30948cda"}}
|
|
reqBodyStr := reqBody.Encode()
|
|
req, err := http.NewRequest("POST", "https://slownie.pl/ajax/word/get", bytes.NewBuffer([]byte(reqBodyStr)))
|
|
req.AddCookie(cookie1)
|
|
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
t.Fatal("webrequest failed")
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("codeL %d", resp.StatusCode)
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal("parsing body failed")
|
|
}
|
|
|
|
gotSlownieResponse := slownieResponse{}
|
|
err = json.Unmarshal(body, &gotSlownieResponse)
|
|
if err != nil {
|
|
t.Fatal("unmarshaling body failed", err)
|
|
return
|
|
}
|
|
|
|
if gotSlownieResponse.Error != 0 {
|
|
t.Fatalf("got error: %d", gotSlownieResponse.Error)
|
|
}
|
|
|
|
their := space.ReplaceAllString(gotSlownieResponse.Data.Word, " ")
|
|
if their != kwota {
|
|
t.Errorf("Using: %.4f %.2f\n", x, x)
|
|
t.Errorf("Got: %s\n", gotSlownieResponse.Data.Word)
|
|
t.Errorf("Own: %s\n", kwota)
|
|
} else {
|
|
|
|
}
|
|
} else {
|
|
t.Errorf("%.2f %s\n", x, err)
|
|
}
|
|
|
|
}
|
|
|
|
func TestKwotaSlownie(t *testing.T) {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
x := r.Float64() * 100000000000
|
|
singleTest(t, x)
|
|
}
|
|
singleTest(t, 1001)
|
|
singleTest(t, 1)
|
|
}
|