golang-client/openstack/session_test.go

60 lines
1.6 KiB
Go

// session_test - REST client session tests
// Copyright 2015 Dean Troyer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openstack_test
import (
"encoding/json"
"net/http"
"testing"
"git.openstack.org/stackforge/golang-client.git/openstack"
"git.openstack.org/stackforge/golang-client.git/testUtil"
)
type TestStruct struct {
ID string `json:"id"`
Name string `json:"name"`
}
func TestSessionGet(t *testing.T) {
tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb"
var apiServer = testUtil.CreateGetJsonTestServer(
t,
tokn,
`{"id":"id1","name":"Chris"}`,
nil,
)
expected := TestStruct{ID: "id1", Name: "Chris"}
actual := TestStruct{}
s, _ := openstack.NewSession(nil, "", nil)
var headers http.Header = http.Header{}
headers.Set("X-Auth-Token", tokn)
headers.Set("Accept", "application/json")
headers.Set("Etag", "md5hash-blahblah")
resp, err := s.Get(apiServer.URL, nil, &headers)
if err != nil {
t.Error(err)
}
testUtil.IsNil(t, err)
if err = json.Unmarshal(resp.Body, &actual); err != nil {
t.Error(err)
}
testUtil.Equals(t, expected, actual)
}