Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jun 12, 2024
1 parent 746b821 commit 1db018b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ jobs:
PDCP_API_KEY: "${{ secrets.PDCP_API_KEY }}"


- name: Running example
- name: Testing Example - Simple
run: go run .
working-directory: examples/
working-directory: examples/simple/
- name: Testing Example - Speed Control
run: go run .
working-directory: examples/speed_control/

- name: Integration Tests Linux, macOS
if: runner.os == 'Linux' || runner.os == 'macOS'
Expand Down
1 change: 0 additions & 1 deletion examples/example.go → examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func main() {
options := runner.Options{
Methods: "GET",
InputTargetHost: goflags.StringSlice{"scanme.sh", "projectdiscovery.io", "localhost"},
//InputFile: "./targetDomains.txt", // path to file containing the target domains list
OnResult: func(r runner.Result) {
// handle error
if r.Err != nil {
Expand Down
99 changes: 99 additions & 0 deletions examples/speed_control/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"time"

"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/httpx/runner"
)

func main() {
gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose) // increase the verbosity (optional)

// generate urls
var urls []string
for i := 0; i < 100; i++ {
urls = append(urls, fmt.Sprintf("https://scanme.sh/a=%d", i))
}

apiEndpoint := "127.0.0.1:31234"

options := runner.Options{
Methods: "GET",
InputTargetHost: goflags.StringSlice(urls),
Threads: 1,
HttpApiEndpoint: apiEndpoint,
OnResult: func(r runner.Result) {
// handle error
if r.Err != nil {
fmt.Printf("[Err] %s: %s\n", r.Input, r.Err)
return
}
fmt.Printf("%s %s %d\n", r.Input, r.Host, r.StatusCode)
},
}

// after 3 seconds increase the speed to 50
time.AfterFunc(3*time.Second, func() {
client := &http.Client{}

concurrencySettings := runner.Concurrency{Threads: 50}
requestBody, err := json.Marshal(concurrencySettings)
if err != nil {
log.Fatalf("Error creating request body: %v", err)
}

req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s/api/concurrency", apiEndpoint), bytes.NewBuffer(requestBody))
if err != nil {
log.Fatalf("Error creating PUT request: %v", err)
}
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending PUT request: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Printf("Failed to update threads, status code: %d", resp.StatusCode)
} else {
log.Println("Threads updated to 50 successfully")
}
})

if err := options.ValidateOptions(); err != nil {
log.Fatal(err)
}

httpxRunner, err := runner.New(&options)
if err != nil {
log.Fatal(err)
}
defer httpxRunner.Close()

httpxRunner.RunEnumeration()

// check the threads
req, err := http.Get(fmt.Sprintf("http://%s/api/concurrency", apiEndpoint))
if err != nil {
log.Fatalf("Error creating GET request: %v", err)
}
var concurrencySettings runner.Concurrency
if err := json.NewDecoder(req.Body).Decode(&concurrencySettings); err != nil {
log.Fatalf("Error decoding response body: %v", err)
}

if concurrencySettings.Threads == 50 {
log.Println("Threads are set to 50")
} else {
log.Fatalf("Fatal error: Threads are not set to 50, current value: %d", concurrencySettings.Threads)
}
}

0 comments on commit 1db018b

Please sign in to comment.