示例#1
0
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")
}
示例#2
0
func TestOnFoundEntry(t *testing.T) {
	assert := assert.New(t)

	// given
	aFile, error := ioutil.TempFile("", "TestNewFileWriterOutputFileDefined")
	if nil != error {
		log.Fatalf("Can't create a temporary output file: %v", error)
	}
	path := aFile.Name()

	builder := configuration.NewBuilder()
	configuration := builder.
		OutputFile(path).
		Build()
	firstFoundEntry := messages.NewFoundEntry("1", "2", 3)
	sut := NewFileWriter(configuration)

	// when
	sut.OnFoundEntry(firstFoundEntry)
	defer cleanAndClose(aFile)

	fi, error := sut.outputFile.Stat()
	if nil != error {
		log.Fatalf("Error occured while checking size of file \"%v\": %v", path, error)
	}

	// then
	assert.True(fi.Size() > 0, "File should have contents")
}
示例#3
0
func TestOnEntry(t *testing.T) {
	// given
	foundEntry := messages.NewFoundEntry("1", "2", 3)
	sut := NewPrinter()

	// when
	sut.OnFoundEntry(foundEntry)
}
示例#4
0
// OnEntry perfoms the main URL check.
func (u *URLChecker) OnEntry(entry messages.Entry) {
	absoluteURL, error := u.baseURL.Parse(entry.RelativeURL())
	if nil != error {
		log.Fatalf("Could not build absolute URL, base URL: %v, relative URL: %v, error: %v", u.baseURL, entry.RelativeURL(), error)
	}

	request := u.createRequest(entry.HTTPMethod(), absoluteURL.String())
	response, error := u.client.Do(request)
	if nil == error {
		defer func() {
			if nil != response.Body {
				response.Body.Close()
			}
		}()

		if response.StatusCode != u.httpErrorCode {
			foundEntry := messages.NewFoundEntry(absoluteURL.String(), entry.HTTPMethod(), response.StatusCode)
			u.FoundEntry <- foundEntry
		}
	}

	u.waitIfNecessary()
}