func TestOnEntryNoURLsFound(t *testing.T) { assert := assert.New(t) // given address := "http://localhost:10606" url, error := url.Parse(address) if nil != error { log.Fatalf("Error occured while parsing an URL: %v, error: %v", address, error) } firstEntry := messages.NewEntry("/aaa", "GET") secondEntry := messages.NewEntry("/bbb", "POST") builder := configuration.NewBuilder() configuration := builder. URLResponseTimeout(3 * time.Second). WorkerWaitPeriod(0). HTTPErrorCode(uint64(http.StatusNotFound)). BaseURL(url). Build() sut := NewURLChecker(configuration) foundEntries := make(chan messages.FoundEntry, 4) assignChannel(sut, foundEntries) // when sut.OnEntry(firstEntry) sut.OnEntry(secondEntry) // then assert.Len(foundEntries, 0, "No entries should be considered as found") }
func TestOnEntryURLsFound(t *testing.T) { assert := assert.New(t) // given scheme := "http" hostPort := "127.0.0.1:10608" server := localserver.NewLocalServer(hostPort, scheme) firstRegisteredPattern := "/aaa" secondRegisteredPattern := "/bbb" http.HandleFunc(firstRegisteredPattern, noOperationHandler) http.HandleFunc(secondRegisteredPattern, noOperationHandler) server.Start() address := scheme + "://" + hostPort url, error := url.Parse(address) if nil != error { log.Fatalf("Error occured while parsing an URL: %v, error: %v", address, error) } firstEntry := messages.NewEntry(firstRegisteredPattern, "GET") secondEntry := messages.NewEntry(secondRegisteredPattern, "POST") thirdEntry := messages.NewEntry("/ccc", "POST") builder := configuration.NewBuilder() configuration := builder. URLResponseTimeout(3 * time.Second). WorkerWaitPeriod(0). HTTPErrorCode(http.StatusNotFound). BaseURL(url). Build() sut := NewURLChecker(configuration) foundEntries := make(chan messages.FoundEntry, 4) assignChannel(sut, foundEntries) // when sut.OnEntry(firstEntry) sut.OnEntry(secondEntry) sut.OnEntry(thirdEntry) server.Stop() http.DefaultServeMux = http.NewServeMux() // then assert.Len(foundEntries, 2, "Two entries should be considered as found") assert.Equal(messages.NewFoundEntry(address+firstEntry.RelativeURL(), firstEntry.HTTPMethod(), http.StatusOK), <-foundEntries, "First entry should be found") assert.Equal(messages.NewFoundEntry(address+secondEntry.RelativeURL(), secondEntry.HTTPMethod(), http.StatusOK), <-foundEntries, "Second entry should be found") }
func TestOnRelativeURLThreeMethods(t *testing.T) { assert := assert.New(t) // given anURL := "an_url" entries := make(chan messages.Entry, 3) methods := []string{"method_a", "method_b", "method_c"} builder := configuration.NewBuilder() configuration := builder.Methods(methods).Build() sut := NewEntryProducer(configuration) sut.Entry = entries // when sut.OnRelativeURL(anURL) // then assert.Equal(<-entries, messages.NewEntry(anURL, "method_a"), "Entries are not equal") assert.Equal(<-entries, messages.NewEntry(anURL, "method_b"), "Entries are not equal") assert.Equal(<-entries, messages.NewEntry(anURL, "method_c"), "Entries are not equal") assert.Len(entries, 0, "Entries channel should be empty now") }
// OnRelativeURL transforms incoming relative URLs into fuzz entries by adding HTTP methods. func (e *EntryProducer) OnRelativeURL(relativeURL string) { for _, httpMethod := range e.methods { entry := messages.NewEntry(relativeURL, httpMethod) e.Entry <- entry } }