github.com/fastwego/offiaccount@v1.0.1/apis/util/util_test.go (about)

     1  // Copyright 2020 FastWeGo
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package util
    16  
    17  import (
    18  	"net/http"
    19  	"os"
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/fastwego/offiaccount"
    24  	"github.com/fastwego/offiaccount/test"
    25  )
    26  
    27  func TestMain(m *testing.M) {
    28  	test.Setup()
    29  	os.Exit(m.Run())
    30  }
    31  
    32  func TestGetCallbackIp(t *testing.T) {
    33  	mockResp := map[string][]byte{
    34  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
    35  	}
    36  	var resp []byte
    37  	test.MockSvrHandler.HandleFunc(apiGetCallbackIp, func(w http.ResponseWriter, r *http.Request) {
    38  		w.Write([]byte(resp))
    39  	})
    40  
    41  	type args struct {
    42  		ctx *offiaccount.OffiAccount
    43  	}
    44  	tests := []struct {
    45  		name     string
    46  		args     args
    47  		wantResp []byte
    48  		wantErr  bool
    49  	}{
    50  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
    51  	}
    52  	for _, tt := range tests {
    53  		t.Run(tt.name, func(t *testing.T) {
    54  			resp = mockResp[tt.name]
    55  			gotResp, err := GetCallbackIp(tt.args.ctx)
    56  			//fmt.Println(string(gotResp), err)
    57  			if (err != nil) != tt.wantErr {
    58  				t.Errorf("GetCallbackIp() error = %v, wantErr %v", err, tt.wantErr)
    59  				return
    60  			}
    61  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
    62  				t.Errorf("GetCallbackIp() gotResp = %v, want %v", gotResp, tt.wantResp)
    63  			}
    64  		})
    65  	}
    66  }
    67  func TestGetApiDomainIp(t *testing.T) {
    68  	mockResp := map[string][]byte{
    69  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
    70  	}
    71  	var resp []byte
    72  	test.MockSvrHandler.HandleFunc(apiGetApiDomainIp, func(w http.ResponseWriter, r *http.Request) {
    73  		w.Write([]byte(resp))
    74  	})
    75  
    76  	type args struct {
    77  		ctx *offiaccount.OffiAccount
    78  	}
    79  	tests := []struct {
    80  		name     string
    81  		args     args
    82  		wantResp []byte
    83  		wantErr  bool
    84  	}{
    85  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			resp = mockResp[tt.name]
    90  			gotResp, err := GetApiDomainIp(tt.args.ctx)
    91  			//fmt.Println(string(gotResp), err)
    92  			if (err != nil) != tt.wantErr {
    93  				t.Errorf("GetApiDomainIp() error = %v, wantErr %v", err, tt.wantErr)
    94  				return
    95  			}
    96  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
    97  				t.Errorf("GetApiDomainIp() gotResp = %v, want %v", gotResp, tt.wantResp)
    98  			}
    99  		})
   100  	}
   101  }
   102  func TestCallbackCheck(t *testing.T) {
   103  	mockResp := map[string][]byte{
   104  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
   105  	}
   106  	var resp []byte
   107  	test.MockSvrHandler.HandleFunc(apiCallbackCheck, func(w http.ResponseWriter, r *http.Request) {
   108  		w.Write([]byte(resp))
   109  	})
   110  
   111  	type args struct {
   112  		ctx     *offiaccount.OffiAccount
   113  		payload []byte
   114  	}
   115  	tests := []struct {
   116  		name     string
   117  		args     args
   118  		wantResp []byte
   119  		wantErr  bool
   120  	}{
   121  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
   122  	}
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			resp = mockResp[tt.name]
   126  			gotResp, err := CallbackCheck(tt.args.ctx, tt.args.payload)
   127  			//fmt.Println(string(gotResp), err)
   128  			if (err != nil) != tt.wantErr {
   129  				t.Errorf("CallbackCheck() error = %v, wantErr %v", err, tt.wantErr)
   130  				return
   131  			}
   132  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
   133  				t.Errorf("CallbackCheck() gotResp = %v, want %v", gotResp, tt.wantResp)
   134  			}
   135  		})
   136  	}
   137  }
   138  func TestClearQuota(t *testing.T) {
   139  	mockResp := map[string][]byte{
   140  		"case1": []byte("{\"errcode\":0,\"errmsg\":\"ok\"}"),
   141  	}
   142  	var resp []byte
   143  	test.MockSvrHandler.HandleFunc(apiClearQuota, func(w http.ResponseWriter, r *http.Request) {
   144  		w.Write([]byte(resp))
   145  	})
   146  
   147  	type args struct {
   148  		ctx     *offiaccount.OffiAccount
   149  		payload []byte
   150  	}
   151  	tests := []struct {
   152  		name     string
   153  		args     args
   154  		wantResp []byte
   155  		wantErr  bool
   156  	}{
   157  		{name: "case1", args: args{ctx: test.MockOffiAccount}, wantResp: mockResp["case1"], wantErr: false},
   158  	}
   159  	for _, tt := range tests {
   160  		t.Run(tt.name, func(t *testing.T) {
   161  			resp = mockResp[tt.name]
   162  			gotResp, err := ClearQuota(tt.args.ctx, tt.args.payload)
   163  			//fmt.Println(string(gotResp), err)
   164  			if (err != nil) != tt.wantErr {
   165  				t.Errorf("ClearQuota() error = %v, wantErr %v", err, tt.wantErr)
   166  				return
   167  			}
   168  			if !reflect.DeepEqual(gotResp, tt.wantResp) {
   169  				t.Errorf("ClearQuota() gotResp = %v, want %v", gotResp, tt.wantResp)
   170  			}
   171  		})
   172  	}
   173  }