82 lines
3.0 KiB
Go
82 lines
3.0 KiB
Go
package swt
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestFetchClassificationCatalogUsesSiteSettingHeaders(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if request.URL.Path != "/oc/SiteSetting.aspx" || request.URL.Query().Get("sn") != "token" || request.URL.Query().Get("siteid") != "99917999" || request.URL.Query().Get("act") != "load" {
|
|
t.Fatalf("request = %s", request.URL.String())
|
|
}
|
|
if err := request.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, key := range []string{"sn", "sidkind_share", "colorkind0_share", "colorkind1_share"} {
|
|
if !request.Form.Has(key) {
|
|
t.Fatalf("missing form key %q: %#v", key, request.Form)
|
|
}
|
|
}
|
|
response.Header().Set("r", "load ok")
|
|
response.Header().Set("sidkind_share", "1,在线咨询,0|2,售后服务,1")
|
|
response.Header().Set("colorkind0_share", "0|2")
|
|
response.Header().Set("colorkind1_share", "普通客户|VIP")
|
|
}))
|
|
defer server.Close()
|
|
|
|
catalog, err := NewClient(rewriteTransportClient(server.URL)).FetchClassificationCatalog(context.Background(), testSession())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(catalog.ConversationKinds) != 2 || catalog.ConversationKinds[1].ID != "2" || catalog.ConversationKinds[1].IconIndex != 1 {
|
|
t.Fatalf("conversation kinds = %#v", catalog.ConversationKinds)
|
|
}
|
|
if len(catalog.CustomerColors) != 2 || catalog.CustomerColors[1].Name != "VIP" {
|
|
t.Fatalf("customer colors = %#v", catalog.CustomerColors)
|
|
}
|
|
}
|
|
|
|
func TestClassificationOperationsUseNativeEndpoints(t *testing.T) {
|
|
var paths []string
|
|
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
paths = append(paths, request.URL.Path)
|
|
if err := request.ParseForm(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
switch request.URL.Path {
|
|
case "/oc/SetSidKind.aspx":
|
|
if request.Form.Get("sid") != "sid-1" || request.Form.Get("kind") != "2" {
|
|
t.Fatalf("chat kind form = %#v", request.Form)
|
|
}
|
|
case "/oc/changecolor.aspx":
|
|
if request.Form.Get("sid") != "sid-1" || request.Form.Get("c0") != "2" || request.Form.Get("c1") != "VIP" || request.Form.Get("cid") != "cid-1" {
|
|
t.Fatalf("customer color form = %#v", request.Form)
|
|
}
|
|
default:
|
|
t.Fatalf("unexpected path %q", request.URL.Path)
|
|
}
|
|
response.Header().Set("r", "ok")
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(rewriteTransportClient(server.URL))
|
|
if err := client.SetConversationKind(context.Background(), testSession(), "sid-1", "2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := client.ChangeCustomerColor(context.Background(), testSession(), "sid-1", "2", "VIP", "cid-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(paths) != 2 || paths[0] != "/oc/SetSidKind.aspx" || paths[1] != "/oc/changecolor.aspx" {
|
|
t.Fatalf("paths = %#v", paths)
|
|
}
|
|
}
|
|
|
|
func TestParseCustomerColorsRejectsMismatchedParallelArrays(t *testing.T) {
|
|
if _, err := parseCustomerColors("0|1", "普通"); err == nil {
|
|
t.Fatal("expected mismatched arrays to fail")
|
|
}
|
|
}
|