~mattfarina/golang-client/master

« back to all changes in this revision

Viewing changes to openstack/session_test.go

  • Committer: Dean Troyer
  • Date: 2015-04-18 06:24:46 UTC
  • Revision ID: git-v1:a279956280f699ba60c580881314fe4132bdfb57
Add Session as base REST interface

This is the initial implementation of a Session object that handles
the REST calls similar to the new Session in python-keystoneclient.
It will be expanded to utilize a callback to an appropriate authentication
handler to re-authenticate as required.

This is intended to replace CallAPI in the util/util package.

Change-Id: I585968cc584327427da3429ef7005dd909c8b8b0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// session_test - REST client session tests
 
2
// Copyright 2015 Dean Troyer
 
3
//
 
4
// Licensed under the Apache License, Version 2.0 (the "License");
 
5
// you may not use this file except in compliance with the License.
 
6
// You may obtain a copy of the License at
 
7
//
 
8
//    http://www.apache.org/licenses/LICENSE-2.0
 
9
//
 
10
// Unless required by applicable law or agreed to in writing, software
 
11
// distributed under the License is distributed on an "AS IS" BASIS,
 
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
// See the License for the specific language governing permissions and
 
14
// limitations under the License.
 
15
 
 
16
 
 
17
package session_test
 
18
 
 
19
import (
 
20
    "encoding/json"
 
21
    "net/http"
 
22
    "testing"
 
23
 
 
24
    "git.openstack.org/stackforge/golang-client.git/openstack"
 
25
    "git.openstack.org/stackforge/golang-client.git/testUtil"
 
26
)
 
27
 
 
28
type TestStruct struct {
 
29
    ID   string `json:"id"`
 
30
    Name string `json:"name"`
 
31
}
 
32
 
 
33
func TestSessionGet(t *testing.T) {
 
34
    tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb"
 
35
    var apiServer = testUtil.CreateGetJsonTestServer(
 
36
        t,
 
37
        tokn,
 
38
        `{"id":"id1","name":"Chris"}`,
 
39
        nil,
 
40
    )
 
41
    expected := TestStruct{ID: "id1", Name: "Chris"}
 
42
    actual := TestStruct{}
 
43
 
 
44
    s, _ := session.NewSession(nil, "", nil)
 
45
    var headers http.Header = http.Header{}
 
46
    headers.Set("X-Auth-Token", tokn)
 
47
    headers.Set("Accept", "application/json")
 
48
    headers.Set("Etag", "md5hash-blahblah")
 
49
    resp, err := s.Get(apiServer.URL, nil, &headers)
 
50
    if err != nil {
 
51
        t.Error(err)
 
52
    }
 
53
    testUtil.IsNil(t, err)
 
54
 
 
55
    if err = json.Unmarshal(resp.Body, &actual); err != nil {
 
56
        t.Error(err)
 
57
    }
 
58
 
 
59
    testUtil.Equals(t, expected, actual)
 
60
}