package webutils

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

	"github.com/stretchr/testify/assert"
)

func TestGet(t *testing.T) {
	resp, err := Get("https://ip.sb", time.Second*3)
	if err != nil {
		t.Fatal(err)
	}

	assert.NotEmpty(t, resp)
}

func TestPostJson(t *testing.T) {
	type ReqData struct {
		Username string
	}

	reqData := ReqData{
		Username: "123",
	}

	dataByte, _ := json.Marshal(reqData)

	_, err := Post("https://ip.sb", string(dataByte), time.Second*3)
	assert.Error(t, err)
}

func TestGetResp(t *testing.T) {
	type DataItem struct {
		API  string   `json:"api"`
		V    string   `json:"v"`
		Ret  []string `json:"ret"`
		Data struct {
			T string `json:"t"`
		} `json:"data"`
	}

	reqUrl := "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp"
	resp, err := GetResp[DataItem](reqUrl, time.Second*3)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Println(resp)
}
