Skip to content

Commit

Permalink
test device.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruizeng committed Nov 26, 2015
1 parent ad052fb commit 2d7d28d
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 7 deletions.
144 changes: 144 additions & 0 deletions tests/device/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"encoding/hex"
"encoding/json"
"fmt"
)

// device register args
type DeviceRegisterArgs struct {
ProductKey string `json:"product_key" binding:"required"`
DeviceCode string `json:"device_code" binding:"required"`
Version string `json:"version" binding:"required"`
}

// device authentication args
type DeviceAuthArgs struct {
DeviceId int64 `json:"device_id" binding:"required"`
DeviceSecret string `json:"device_secret" binding:"required"`
Protocol string `json:"protocol" binding:"required"`
}

// common response fields
type Common struct {
Code int `json:"code"`
Message string `json:"message"`
}

// device register response data field
type DeviceRegisterData struct {
DeviceId int64 `json:"device_id"`
DeviceSecret string `json:"device_secret"`
DeviceKey string `json:"device_key"`
DeviceIdentifier string `json:"device_identifier"`
}

// device register response
type DeviceRegisterResponse struct {
Common
Data DeviceRegisterData `json:"data"`
}

// device auth response data field
type DeviceAuthData struct {
AccessToken string `json:"access_token"`
AccessAddr string `json:"access_addr"`
}

// device auth response
type DeviceAuthResponse struct {
Common
Data DeviceAuthData `json:"data"`
}

type Device struct {
// API URL
BrokerUrl string

// basic info
ProductKey string
DeviceCode string
Version string

// private thins
id int64
secrect string
token []byte
access string
}

func NewDevice(broker string, productkey string, code string, version string) *Device {
return &Device{
BrokerUrl: broker,
ProductKey: productkey,
DeviceCode: code,
Version: version,
}
}

func (d *Device) DoRegister() error {
args := DeviceRegisterArgs{
ProductKey: d.ProductKey,
DeviceCode: d.DeviceCode,
Version: d.Version,
}
regUrl := fmt.Sprintf("%v%v", d.BrokerUrl, "/v1/devices/registration")
request, err := json.Marshal(args)
if err != nil {
return err
}
jsonresp, err := SendHttpRequest(regUrl, string(request), "POST", nil)
if err != nil {
return err
}
response := DeviceRegisterResponse{}
err = json.Unmarshal(jsonresp, &response)
if err != nil {
return err
}
err = CheckHttpsCode(response)
if err != nil {
return err
}

d.id = response.Data.DeviceId
d.secrect = response.Data.DeviceSecret

return nil
}

func (d *Device) DoLogin() error {
args := DeviceAuthArgs{
DeviceId: d.id,
DeviceSecret: d.secrect,
Protocol: "http",
}
regUrl := fmt.Sprintf("%v%v", d.BrokerUrl, "/v1/devices/authentication")
request, err := json.Marshal(args)
if err != nil {
return err
}
jsonresp, err := SendHttpRequest(regUrl, string(request), "POST", nil)
if err != nil {
return err
}
response := DeviceAuthResponse{}
err = json.Unmarshal(jsonresp, &response)
if err != nil {
return err
}
err = CheckHttpsCode(response)
if err != nil {
return err
}
// ecode hex
htoken, err := hex.DecodeString(response.Data.AccessToken)
if err != nil {
return err
}
d.token = htoken
d.access = response.Data.AccessAddr

return nil
}
27 changes: 27 additions & 0 deletions tests/device/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
)

const (
TestBroker = "https://localhost"
TestProductKey = "aec003c9018b9a572ceb19720e589c375ead1a2b1fbd0d089d067128611754ff"
)

func main() {
dev := NewDevice(TestBroker, TestProductKey, "ffe34e", "version")

err := dev.DoRegister()
if err != nil {
fmt.Errorf("device register error %s", err)
return
}

err = dev.DoLogin()
if err != nil {
fmt.Errorf("device login error %s", err)
return
}

}
17 changes: 10 additions & 7 deletions tests/device/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func SendHttpRequest(argUrl string, argReq string, argType string, argHead map[s
bReq := []byte(argReq)
req, err := http.NewRequest(argType, argUrl, bytes.NewBuffer(bReq))
if err != nil {
panic(err)
return nil, err
}
for key, vaule := range argHead {
req.Header.Set(key, vaule)
Expand All @@ -40,7 +40,7 @@ func SendHttpRequest(argUrl string, argReq string, argType string, argHead map[s
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
panic(err)
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -49,21 +49,24 @@ func SendHttpRequest(argUrl string, argReq string, argType string, argHead map[s
}

/**
assert the https response code
check the https response code
*/
func AssertHttpsCode(resp interface{}) {
func CheckHttpsCode(resp interface{}) error {
res := reflect.ValueOf(resp)
// struct
if res.Kind() == reflect.Struct {
// exported field
f := res.FieldByName("Code")
if f.IsValid() {
if f.Interface() == 0 {
return
return nil
} else {
err := errors.New("[Https Response Error]code is not equal to 0!")
panic(err)
msg := res.FieldByName("Message")
err := errors.New(msg.Interface().(string))
return err
}
}
}

return errors.New("response format error")
}

0 comments on commit 2d7d28d

Please sign in to comment.