Files
CUSIMInfo/internal/unicom/script_test.go
T

107 lines
4.1 KiB
Go

package unicom
import (
"strings"
"testing"
)
func TestJSONPCallbackName(t *testing.T) {
name := jsonpCallback()
if !strings.HasPrefix(name, "jQuery") || !strings.Contains(name, "_") {
t.Fatal(name)
}
}
func TestParseJSONP(t *testing.T) {
quoted, err := parseJSONP(`cb({"resultCode":"false","ckCode":"2"});`)
if err != nil || asString(quoted["resultCode"]) != "false" {
t.Fatalf("quoted %#v %v", quoted, err)
}
bare, err := parseJSONP(`jQuery1({resultCode:"0000",redirectURL:"null"});`)
if err != nil || asString(bare["resultCode"]) != "0000" || asString(bare["redirectURL"]) != "null" {
t.Fatalf("bare %#v %v", bare, err)
}
}
func TestParseLoginBundle(t *testing.T) {
js := `
RSAUtils.getKeyPair("11", "", "abc123");
new TencentCaptcha('197439193', callback);
CommonConstants.SEND_RNDCODE_URL = UacPrefix.PRXFIX + "/portal/Service/SendMSG?callback=?";
CommonConstants.LOGIN_URL = UacPrefix.PRXFIX + "/portal/Service/MallLogin?callback=?";
var genqrimg = UacPrefix.PRXFIX + "/oauth2/genqr";
var heartbeaturl = UacPrefix.PRXFIX + "/qrcode/qrcode_hbt?callback=?";
var logonurl = UacPrefix.PRXFIX + "/portal/qrcode/qrcode_logon?callback=?";
params.secsnid = $.cookie("unisecid");
setCookie("piw", data.piw);
CommonConstants.PWD_TYPE_RND = "02";
CommonConstants.USER_TYPE_MOBILE = "01";
`
got, err := parseLoginBundle(js)
if err != nil {
t.Fatal(err)
}
if got.exponent != "11" || got.modulus != "abc123" || got.captchaAppID != "197439193" {
t.Fatalf("key %#v", got)
}
if got.sendPath != "/portal/Service/SendMSG" || got.loginPath != "/portal/Service/MallLogin" {
t.Fatalf("paths %#v", got)
}
if got.genQRPath != "/oauth2/genqr" || got.heartPath != "/qrcode/qrcode_hbt" || got.logonPath != "/portal/qrcode/qrcode_logon" {
t.Fatalf("qr %#v", got)
}
if got.secCookie != "unisecid" || got.piwCookie != "piw" || got.pwdType != "02" || got.userType != "01" {
t.Fatalf("names %#v", got)
}
html := `<input type="hidden" id="loginFrom" name="loginFrom" value='01' />
<script src="https://turing.captcha.qcloud.com/TJCaptcha.js"></script>`
if parseLoginFrom(html) != "01" {
t.Fatal(parseLoginFrom(html))
}
if parseCaptchaScript(html) != "https://turing.captcha.qcloud.com/TJCaptcha.js" {
t.Fatal(parseCaptchaScript(html))
}
}
func TestParseBillAPI(t *testing.T) {
js := `VUE_APP_XX_BASE_URL:"https://bill.example.10010.com",VUE_APP_KEY_QUERY:"servicequerybusiness",version:"WT",language:"chinese"
A(B+"/balancenew/accountBalancenew.htm",t)`
api, ok := parseBillAPI(js, "https://img.example.10010.com/fee/index.html", "/balancenew/accountBalancenew.htm")
if !ok {
t.Fatal("not parsed")
}
if api.URL != "https://bill.example.10010.com/servicequerybusiness/balancenew/accountBalancenew.htm" {
t.Fatal(api.URL)
}
if api.Version != "WT" || api.Language != "chinese" || api.Origin != "https://img.example.10010.com" {
t.Fatalf("%#v", api)
}
}
func TestParseBillLanguageType(t *testing.T) {
js := `VUE_APP_XX_BASE_URL:"https://bill.example.10010.com",key_query:"servicequerybusiness",version:"WT",languageType="chinese"
m("".concat(g.c,"/operationservice/queryOcsPackageFlowLeftContentRevisedInJune"),e)`
api, ok := parseBillAPI(js, "https://img.example.10010.com/plan/index.html", "/operationservice/queryOcsPackageFlowLeftContentRevisedInJune")
if !ok || api.Language != "chinese" {
t.Fatalf("%v %#v", ok, api)
}
}
func TestResolveNav(t *testing.T) {
texts := []string{
`function getNavJson(){ var url = CommonsVariables.QueryRequestPrefix() +'/QueryNavigations'; }`,
`QueryRequestPrefix : function() { return "//www.10010.com/mall/service/query"; },`,
}
if got := resolveNav(texts); got != "https://www.10010.com/mall/service/query/QueryNavigations" {
t.Fatal(got)
}
}
func TestMenuPages(t *testing.T) {
raw := []byte(`{"list":[{"title":"剩余话费","url":"https://img.example.10010.com/fee/index.html"},{"title":"余量查询","url":"https://img.example.10010.com/plan/index.html"}]}`)
fee, plan := menuPages(raw)
if len(fee) != 1 || fee[0] != "https://img.example.10010.com/fee/index.html" || len(plan) != 1 || plan[0] != "https://img.example.10010.com/plan/index.html" {
t.Fatalf("%v %v", fee, plan)
}
}