golang-client/misc/util_test.go
Matt Farina 83c8761d52 Adding the appropriate license and copyright statements.
Change-Id: Iae495e0b7f6b3dae0de95d9fa3e6224311bff5e9
2014-05-29 17:09:57 -04:00

111 lines
3.3 KiB
Go

// Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
//
// 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 misc_test
import (
"bytes"
"errors"
"git.openstack.org/stackforge/golang-client.git/misc"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func TestCallAPI(t *testing.T) {
tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb"
var apiServer = httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Auth-Token") != tokn {
t.Error(errors.New("Token failed"))
}
w.WriteHeader(200) //ok
}))
zeroByte := &([]byte{})
if _, err := misc.CallAPI("HEAD", apiServer.URL, zeroByte, "X-Auth-Token", tokn); err != nil {
t.Error(err)
}
if _, err := misc.CallAPI("DELETE", apiServer.URL, zeroByte, "X-Auth-Token", tokn); err != nil {
t.Error(err)
}
if _, err := misc.CallAPI("POST", apiServer.URL, zeroByte, "X-Auth-Token", tokn); err != nil {
t.Error(err)
}
}
func TestCallAPIGetContent(t *testing.T) {
tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb"
fContent, err := ioutil.ReadFile("./util.go")
if err != nil {
t.Error(err)
}
var apiServer = httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if r.Header.Get("X-Auth-Token") != tokn {
t.Error(errors.New("Token failed"))
}
w.Header().Set("Content-Length", r.Header.Get("Content-Length"))
w.Write(body)
}))
var resp *http.Response
if resp, err = misc.CallAPI("GET", apiServer.URL, &fContent, "X-Auth-Token", tokn,
"Etag", "md5hash-blahblah"); err != nil {
t.Error(err)
}
if strconv.Itoa(len(fContent)) != resp.Header.Get("Content-Length") {
t.Error(errors.New("Failed: Content-Length comparison"))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if !bytes.Equal(fContent, body) {
t.Error(errors.New("Failed: Content body comparison"))
}
}
func TestCallAPIPutContent(t *testing.T) {
tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb"
fContent, err := ioutil.ReadFile("./util.go")
if err != nil {
t.Error(err)
}
var apiServer = httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Auth-Token") != tokn {
t.Error(errors.New("Token failed"))
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if strconv.Itoa(len(fContent)) != r.Header.Get("Content-Length") {
t.Error(errors.New("Failed: Content-Length comparison"))
}
if !bytes.Equal(fContent, body) {
t.Error(errors.New("Failed: Content body comparison"))
}
w.WriteHeader(200)
}))
if _, err = misc.CallAPI("PUT", apiServer.URL, &fContent, "X-Auth-Token", tokn); err != nil {
t.Error(err)
}
}