import (
"encoding/json"
+ "io"
"io/ioutil"
"k8splugin/internal/rb"
"net/http"
func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Request) {
var v rb.Definition
- if r.Body == nil {
+ err := json.NewDecoder(r.Body).Decode(&v)
+ switch {
+ case err == io.EOF:
http.Error(w, "Empty body", http.StatusBadRequest)
return
- }
-
- err := json.NewDecoder(r.Body).Decode(&v)
- if err != nil {
+ case err != nil:
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
vars := mux.Vars(r)
uuid := vars["rbdID"]
- if r.Body == nil {
- http.Error(w, "Empty Body", http.StatusBadRequest)
- return
- }
-
inpBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
+ if len(inpBytes) == 0 {
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ }
+
err = h.client.Upload(uuid, inpBytes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbDefinitionHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("POST", "/v1/rb/definition", testCase.reader)
-
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("POST", "/v1/rb/definition", testCase.reader)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.createHandler)
- hr.ServeHTTP(rr, req)
+ vh.createHandler(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusCreated
- if rr.Code == http.StatusCreated {
+ if resp.StatusCode == http.StatusCreated {
got := rb.Definition{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
t.Errorf("createHandler returned unexpected body: got %v;"+
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbDefinitionHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/definition", nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/definition", nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.listHandler)
+ vh.listHandler(rr, req)
+ resp := rr.Result()
- hr.ServeHTTP(rr, req)
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusOK
- if rr.Code == http.StatusOK {
+ if resp.StatusCode == http.StatusOK {
got := []rb.Definition{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
// Since the order of returned slice is not guaranteed
// Check both and return error if both don't match
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbDefinitionHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.getHandler)
-
- hr.ServeHTTP(rr, req)
+ vh.getHandler(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusOK
- if rr.Code == http.StatusOK {
+ if resp.StatusCode == http.StatusOK {
got := rb.Definition{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
t.Errorf("listHandler returned unexpected body: got %v;"+
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbDefinitionHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.deleteHandler)
-
- hr.ServeHTTP(rr, req)
+ vh.deleteHandler(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
})
}
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbDefinitionHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("POST",
+ req := httptest.NewRequest("POST",
"/v1/rb/definition/"+testCase.inpUUID+"/content", testCase.body)
-
- if err != nil {
- t.Fatal(err)
- }
-
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.uploadHandler)
-
- hr.ServeHTTP(rr, req)
+ vh.uploadHandler(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
})
}
import (
"encoding/json"
"errors"
+ "io"
"log"
"net/http"
"os"
func CreateHandler(w http.ResponseWriter, r *http.Request) {
var resource CreateVnfRequest
- if r.Body == nil {
+ err := json.NewDecoder(r.Body).Decode(&resource)
+ switch {
+ case err == io.EOF:
http.Error(w, "Body empty", http.StatusBadRequest)
return
- }
-
- err := json.NewDecoder(r.Body).Decode(&resource)
- if err != nil {
+ case err != nil:
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
return c.err
}
-func executeRequest(req *http.Request) *httptest.ResponseRecorder {
+func executeRequest(req *http.Request) *http.Response {
router := NewRouter("", nil, nil)
recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)
- return recorder
+ return recorder.Result()
}
func checkResponseCode(t *testing.T, expected, actual int) {
db.DBconn = testCase.mockStore
}
- request, _ := http.NewRequest("POST", "/v1/vnf_instances/", testCase.input)
+ request := httptest.NewRequest("POST", "/v1/vnf_instances/", testCase.input)
result := executeRequest(request)
- if testCase.expectedCode != result.Code {
- t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", result.Code, testCase.expectedCode)
+ if testCase.expectedCode != result.StatusCode {
+ t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", result.StatusCode, testCase.expectedCode)
}
- if result.Code == http.StatusCreated {
+ if result.StatusCode == http.StatusCreated {
var response CreateVnfResponse
err := json.NewDecoder(result.Body).Decode(&response)
if err != nil {
db.DBconn = testCase.mockStore
}
- request, _ := http.NewRequest("GET", "/v1/vnf_instances/cloud1/default", nil)
+ request := httptest.NewRequest("GET", "/v1/vnf_instances/cloud1/default", nil)
result := executeRequest(request)
- if testCase.expectedCode != result.Code {
+ if testCase.expectedCode != result.StatusCode {
t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v",
- result.Code, testCase.expectedCode)
+ result.StatusCode, testCase.expectedCode)
}
- if result.Code == http.StatusOK {
+ if result.StatusCode == http.StatusOK {
var response ListVnfsResponse
err := json.NewDecoder(result.Body).Decode(&response)
if err != nil {
helper.DestroyVNF = testCase.mockDeleteVNF.DestroyVNF
}
- request, _ := http.NewRequest("DELETE", "/v1/vnf_instances/cloudregion1/testnamespace/uuid1", nil)
+ request := httptest.NewRequest("DELETE", "/v1/vnf_instances/cloudregion1/testnamespace/uuid1", nil)
result := executeRequest(request)
- if testCase.expectedCode != result.Code {
- t.Fatalf("Request method returned: %v and it was expected: %v", result.Code, testCase.expectedCode)
+ if testCase.expectedCode != result.StatusCode {
+ t.Fatalf("Request method returned: %v and it was expected: %v", result.StatusCode, testCase.expectedCode)
}
})
}
var result UpdateVnfResponse
- req, _ := http.NewRequest("PUT", "/v1/vnf_instances/1", bytes.NewBuffer(payload))
+ req := httptest.NewRequest("PUT", "/v1/vnf_instances/1", bytes.NewBuffer(payload))
GetVNFClient = func(configPath string) (krd.VNFInstanceClientInterface, error) {
return &mockClient{
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
db.DBconn = testCase.mockStore
- request, _ := http.NewRequest("GET", "/v1/vnf_instances/cloud1/default/1", nil)
+ request := httptest.NewRequest("GET", "/v1/vnf_instances/cloud1/default/1", nil)
result := executeRequest(request)
- if testCase.expectedCode != result.Code {
+ if testCase.expectedCode != result.StatusCode {
t.Fatalf("Request method returned: %v and it was expected: %v",
- result.Code, testCase.expectedCode)
+ result.StatusCode, testCase.expectedCode)
}
- if result.Code == http.StatusOK {
+ if result.StatusCode == http.StatusOK {
var response GetVnfResponse
err := json.NewDecoder(result.Body).Decode(&response)
if err != nil {
import (
"encoding/json"
+ "io"
"io/ioutil"
"k8splugin/internal/rb"
"net/http"
func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) {
var v rb.Profile
- if r.Body == nil {
+ err := json.NewDecoder(r.Body).Decode(&v)
+ switch {
+ case err == io.EOF:
http.Error(w, "Empty body", http.StatusBadRequest)
return
- }
-
- err := json.NewDecoder(r.Body).Decode(&v)
- if err != nil {
+ case err != nil:
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
return
}
vars := mux.Vars(r)
uuid := vars["rbpID"]
- if r.Body == nil {
- http.Error(w, "Empty Body", http.StatusBadRequest)
- return
- }
-
inpBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to read body", http.StatusBadRequest)
return
}
+ if len(inpBytes) == 0 {
+ http.Error(w, "Empty body", http.StatusBadRequest)
+ return
+ }
+
err = h.client.Upload(uuid, inpBytes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbProfileHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("POST", "/v1/rb/profile", testCase.reader)
-
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("POST", "/v1/rb/profile", testCase.reader)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.createHandler)
- hr.ServeHTTP(rr, req)
+ vh.createHandler(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusCreated
- if rr.Code == http.StatusCreated {
+ if resp.StatusCode == http.StatusCreated {
got := rb.Profile{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
t.Errorf("createHandler returned unexpected body: got %v;"+
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbProfileHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/profile", nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/profile", nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.listHandler)
+ vh.listHandler(rr, req)
- hr.ServeHTTP(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusOK
- if rr.Code == http.StatusOK {
+ if resp.StatusCode == http.StatusOK {
got := []rb.Profile{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
// Since the order of returned slice is not guaranteed
// Check both and return error if both don't match
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbProfileHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.getHandler)
+ vh.getHandler(rr, req)
- hr.ServeHTTP(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
//Check returned body only if statusOK
- if rr.Code == http.StatusOK {
+ if resp.StatusCode == http.StatusOK {
got := rb.Profile{}
- json.NewDecoder(rr.Body).Decode(&got)
+ json.NewDecoder(resp.Body).Decode(&got)
if reflect.DeepEqual(testCase.expected, got) == false {
t.Errorf("listHandler returned unexpected body: got %v;"+
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbProfileHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
- if err != nil {
- t.Fatal(err)
- }
-
+ req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.deleteHandler)
+ vh.deleteHandler(rr, req)
- hr.ServeHTTP(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
})
}
for _, testCase := range testCases {
t.Run(testCase.label, func(t *testing.T) {
vh := rbProfileHandler{client: testCase.rbDefClient}
- req, err := http.NewRequest("POST",
+ req := httptest.NewRequest("POST",
"/v1/rb/profile/"+testCase.inpUUID+"/content", testCase.body)
-
- if err != nil {
- t.Fatal(err)
- }
-
rr := httptest.NewRecorder()
- hr := http.HandlerFunc(vh.uploadHandler)
+ vh.uploadHandler(rr, req)
- hr.ServeHTTP(rr, req)
+ resp := rr.Result()
//Check returned code
- if rr.Code != testCase.expectedCode {
- t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+ if resp.StatusCode != testCase.expectedCode {
+ t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
}
})
}