-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec75263
commit 3b7f9c8
Showing
5 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package v2 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
. "github.com/onsi/gomega" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
type HoverflyJournalIndexStub struct{} | ||
|
||
func (HoverflyJournalIndexStub) GetAllIndexes() []JournalIndexView { | ||
|
||
return getJournalIndexes() | ||
} | ||
|
||
func getJournalIndexes() []JournalIndexView { | ||
journalIndexViews := []JournalIndexView{} | ||
journalIndexViews = append(journalIndexViews, JournalIndexView{Name: "Request.QueryParam.id"}) | ||
return journalIndexViews | ||
} | ||
func (HoverflyJournalIndexStub) AddJournalIndex(string) error { | ||
return nil | ||
} | ||
func (HoverflyJournalIndexStub) DeleteJournalIndex(string) { | ||
|
||
} | ||
|
||
func Test_JournalIndexHandler_GetAllJournalIndexes(t *testing.T) { | ||
RegisterTestingT(t) | ||
stubHoverfly := HoverflyJournalIndexStub{} | ||
unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} | ||
|
||
request, err := http.NewRequest("GET", "/api/v2/journal/index", nil) | ||
Expect(err).To(BeNil()) | ||
response := makeRequestOnHandler(unit.GetAll, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
|
||
responseBody := response.Body.String() | ||
expectedResponseBodyBytes, _ := json.Marshal(getJournalIndexes()) | ||
Expect(responseBody).To(Equal(string(expectedResponseBodyBytes))) | ||
|
||
} | ||
|
||
func Test_JournalIndexHandler_SetJournalIndex(t *testing.T) { | ||
RegisterTestingT(t) | ||
stubHoverfly := HoverflyJournalIndexStub{} | ||
unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} | ||
|
||
journalIndexRequest := JournalIndexRequestView{ | ||
Name: "Request.QueryParams.id", | ||
} | ||
bodyBytes, err := json.Marshal(journalIndexRequest) | ||
Expect(err).To(BeNil()) | ||
request, err := http.NewRequest("POST", "/api/v2/journal/index", ioutil.NopCloser(bytes.NewBuffer(bodyBytes))) | ||
|
||
Expect(err).To(BeNil()) | ||
response := makeRequestOnHandler(unit.Post, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
} | ||
|
||
func Test_JournalIndexHandler_DeleteJournalIndex(t *testing.T) { | ||
RegisterTestingT(t) | ||
stubHoverfly := HoverflyJournalIndexStub{} | ||
unit := HoverflyJournalIndexHandler{Hoverfly: stubHoverfly} | ||
request, err := http.NewRequest("DELETE", "/api/v2/journal/index/Request.QueryParams.id", nil) | ||
|
||
Expect(err).To(BeNil()) | ||
response := makeRequestOnHandler(unit.Delete, request) | ||
Expect(response.Code).To(Equal(http.StatusOK)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package journal | ||
|
||
import ( | ||
"github.com/SpectoLabs/hoverfly/core/models" | ||
"github.com/SpectoLabs/hoverfly/core/util" | ||
. "github.com/onsi/gomega" | ||
"testing" | ||
) | ||
|
||
func TestIndex_AddJournalEntry(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
index := Index{ | ||
Name: "Request.QueryParam.id", | ||
Entries: make(map[string]*JournalEntry), | ||
} | ||
|
||
queryParams := make(map[string][]string) | ||
queryParams["id"] = []string{"0100"} | ||
journalEntry := JournalEntry{ | ||
Id: "12345", | ||
Request: &models.RequestDetails{ | ||
Query: queryParams, | ||
}, | ||
} | ||
index.AddJournalEntry(&journalEntry) | ||
Expect(index.Entries).ToNot(BeNil()) | ||
Expect(index.Entries).To(HaveLen(1)) | ||
Expect(index.Entries["0100"]).To(Equal(&journalEntry)) | ||
} | ||
|
||
func TestIndex_AddJournalEntryWithJsonRequestBody(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
index := Index{ | ||
Name: "Request.Body 'jsonpath' '$.name'", | ||
Entries: make(map[string]*JournalEntry), | ||
} | ||
|
||
journalEntry := JournalEntry{ | ||
Id: "12345", | ||
Request: &models.RequestDetails{ | ||
Body: "{\"name\":\"Application Testing\"}", | ||
}, | ||
} | ||
index.AddJournalEntry(&journalEntry) | ||
Expect(index.Entries).ToNot(BeNil()) | ||
Expect(index.Entries).To(HaveLen(1)) | ||
Expect(index.Entries["Application Testing"]).To(Equal(&journalEntry)) | ||
} | ||
|
||
func TestIndex_AddJournalEntryWithXMLRequestBody(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
index := Index{ | ||
Name: "Request.Body 'xpath' '/document/id'", | ||
Entries: make(map[string]*JournalEntry), | ||
} | ||
|
||
journalEntry := JournalEntry{ | ||
Id: "12345", | ||
Request: &models.RequestDetails{ | ||
Body: "<document><id>1234</id><name>Test</name></document>", | ||
}, | ||
} | ||
index.AddJournalEntry(&journalEntry) | ||
Expect(index.Entries).ToNot(BeNil()) | ||
Expect(index.Entries).To(HaveLen(1)) | ||
Expect(index.Entries["1234"]).To(Equal(&journalEntry)) | ||
} | ||
|
||
func TestIndex_ConvertToIndexView(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
journalEntry1 := JournalEntry{ | ||
Id: "1", | ||
} | ||
journalEntry2 := JournalEntry{ | ||
Id: "2", | ||
} | ||
|
||
entries := make(map[string]*JournalEntry) | ||
entries["a"] = &journalEntry1 | ||
entries["b"] = &journalEntry2 | ||
index := Index{ | ||
Name: "Request.QueryParam.id", | ||
Entries: entries, | ||
} | ||
indexView := index.getIndexView() | ||
Expect(indexView).ToNot(BeNil()) | ||
Expect(indexView.Name).To(Equal("Request.QueryParam.id")) | ||
Expect(indexView.Entries).To(HaveLen(2)) | ||
Expect(indexView.Entries[0].Key).To(Equal("a")) | ||
Expect(indexView.Entries[0].JournalEntryId).To(Equal("1")) | ||
Expect(indexView.Entries[1].Key).To(Equal("b")) | ||
Expect(indexView.Entries[1].JournalEntryId).To(Equal("2")) | ||
} | ||
|
||
func TestIndex_FilteredConversionIndexView(t *testing.T) { | ||
RegisterTestingT(t) | ||
journalEntry1 := JournalEntry{ | ||
Id: "1", | ||
} | ||
journalEntry2 := JournalEntry{ | ||
Id: "2", | ||
} | ||
|
||
entries := make(map[string]*JournalEntry) | ||
entries["a"] = &journalEntry1 | ||
entries["b"] = &journalEntry2 | ||
index := Index{ | ||
Name: "Request.QueryParam.id", | ||
Entries: entries, | ||
} | ||
selectedJournalEntryIds := util.NewHashSet() | ||
selectedJournalEntryIds.Add("2") | ||
indexView := index.convertIndex(selectedJournalEntryIds) | ||
Expect(indexView).ToNot(BeNil()) | ||
Expect(indexView.Name).To(Equal("Request.QueryParam.id")) | ||
Expect(indexView.Entries).To(HaveLen(1)) | ||
Expect(indexView.Entries[0].Key).To(Equal("b")) | ||
Expect(indexView.Entries[0].JournalEntryId).To(Equal("2")) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters