|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package weapp
-
- import (
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "testing"
- )
-
- func TestApplyPlugin(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiPlugin {
- t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- params := struct {
- Action string `json:"action"`
- PluginAppID string `json:"plugin_appid"`
- Reason string `json:"reason"`
- }{}
-
- if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
- t.Fatal(err)
- }
-
- if params.Action != "apply" {
- t.Error("Unexpected action")
- }
-
- if params.PluginAppID == "" {
- t.Error("Response column plugin_appid can not be empty")
- }
-
- w.WriteHeader(http.StatusOK)
-
- raw := `{
- "errcode": 0,
- "errmsg": "ok"
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := applyPlugin(ts.URL+apiPlugin, "mock-access-token", "plugin-app-id", "mock-reason")
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestGetPluginDevApplyList(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiDevPlugin {
- t.Fatalf("Except to path '%s',get '%s'", apiDevPlugin, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- params := struct {
- Action string `json:"action"`
- Page uint `json:"page"`
- Number uint `json:"num"`
- }{}
-
- if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
- t.Fatal(err)
- }
-
- if params.Action != "dev_apply_list" {
- t.Error("Unexpected action")
- }
-
- if params.Page == 0 {
- t.Error("Response column page can not be zero")
- }
-
- if params.Number == 0 {
- t.Error("Response column num can not be zero")
- }
-
- w.WriteHeader(http.StatusOK)
-
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "apply_list": [{
- "appid": "xxxxxxxxxxxxx",
- "status": 1,
- "nickname": "名称",
- "headimgurl": "**********",
- "reason": "polo has gone",
- "apply_url": "*******",
- "create_time": "1536305096",
- "categories": [{
- "first": "IT科技",
- "second": "硬件与设备"
- }]
- }]
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := getPluginDevApplyList(ts.URL+apiDevPlugin, "mock-access-token", 1, 2)
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestGetPluginList(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiPlugin {
- t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- params := struct {
- Action string `json:"action"`
- }{}
-
- if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
- t.Fatal(err)
- }
-
- if params.Action != "list" {
- t.Error("Unexpected action")
- }
-
- w.WriteHeader(http.StatusOK)
-
- raw := `{
- "errcode": 0,
- "errmsg": "ok",
- "plugin_list": [{
- "appid": "aaaa",
- "status": 1,
- "nickname": "插件昵称",
- "headimgurl": "http://plugin.qq.com"
- }]
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := getPluginList(ts.URL+apiPlugin, "mock-access-token")
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestSetDevPluginApplyStatus(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiDevPlugin {
- t.Fatalf("Except to path '%s',get '%s'", apiDevPlugin, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- params := struct {
- Action string `json:"action"`
- AppID string `json:"appid"`
- Reason string `json:"reason"`
- }{}
-
- if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
- t.Fatal(err)
- }
-
- if params.Action == "" {
- t.Error("Response column action can not be empty")
- }
-
- w.WriteHeader(http.StatusOK)
-
- raw := `{
- "errcode": 0,
- "errmsg": "ok"
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := setDevPluginApplyStatus(ts.URL+apiDevPlugin, "mock-access-token", "mock-plugin-app-id", "mock-reason", DevAgree)
- if err != nil {
- t.Fatal(err)
- }
- }
-
- func TestUnbindPlugin(t *testing.T) {
- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
- if r.Method != "POST" {
- t.Fatalf("Expect 'POST' get '%s'", r.Method)
- }
-
- path := r.URL.EscapedPath()
- if path != apiPlugin {
- t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
- }
-
- if err := r.ParseForm(); err != nil {
- t.Fatal(err)
- }
-
- if r.Form.Get("access_token") == "" {
- t.Fatalf("access_token can not be empty")
- }
-
- params := struct {
- Action string `json:"action"`
- PluginAppID string `json:"plugin_appid"`
- }{}
-
- if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
- t.Fatal(err)
- }
-
- if params.Action != "unbind" {
- t.Error("Unexpected action")
- }
-
- if params.PluginAppID == "" {
- t.Error("Response column plugin_appid can not be empty")
- }
-
- w.WriteHeader(http.StatusOK)
-
- raw := `{
- "errcode": 0,
- "errmsg": "ok"
- }`
- if _, err := w.Write([]byte(raw)); err != nil {
- t.Fatal(err)
- }
- }))
- defer ts.Close()
-
- _, err := unbindPlugin(ts.URL+apiPlugin, "mock-access-token", "mock-plugin-app-id")
- if err != nil {
- t.Fatal(err)
- }
- }
|