ipinfo_test.go 1.67 KB
Newer Older
qiuqunfeng's avatar
qiuqunfeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
package service

import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

	"gitlab.com/tensorsecurity-rd/waf-console/internal/model"
)

func TestIPService_QueryIP(t *testing.T) {
	type fields struct {
		URL    string
		ApiKey string
	}
	type args struct {
		ip string
	}
	tests := []struct {
		name    string
		fields  fields
		args    args
		want    *model.IPInfo
		wantErr bool
	}{
		// TODO: Add test cases.
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &ipService{
				URL:    tt.fields.URL,
				ApiKey: tt.fields.ApiKey,
			}
			got, err := s.QueryIP(tt.args.ip)
			if (err != nil) != tt.wantErr {
				t.Errorf("IPService.QueryIP() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("IPService.QueryIP() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestParseIPInfo(t *testing.T) {
	t.Run("test 1", func(t *testing.T) {
		var respData RespData
		err := json.Unmarshal([]byte(fakeData), &respData)
		if err != nil {
			t.Errorf("Unmarshal err %v", err)
		}
		if info, ok := respData.Data["87.236.176.199"]; ok {
			var ipInfo IpInfo
			d, err := info.MarshalJSON()
			if err != nil {
				t.Error(err)
			}
			err = json.Unmarshal(d, &ipInfo)
			if err != nil {
				t.Error(err)
			}
			fmt.Println(ipInfo.Judgments)
		}
		fmt.Println("ddddd")

	})
}

func TestParseIPInfoPrivate(t *testing.T) {
	t.Run("test 1", func(t *testing.T) {
		var respData IPInfoPrivateResp
		err := json.Unmarshal([]byte(fakePrivateData), &respData)
		if err != nil {
			t.Errorf("Unmarshal err %v", err)
		}
		if len(respData.Data) > 0 {
			for _, v := range respData.Data {
				fmt.Println(v)
			}
		}
		fmt.Println("ddddd")
	})
}