mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-04 04:23:02 +00:00
update
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package scraper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestOpenAPIScraperScrape(t *testing.T) {
|
||||
spec := `{
|
||||
"openapi": "3.0.0",
|
||||
"info": {"title": "Pet API", "version": "1.0.0"},
|
||||
"paths": {
|
||||
"/pets": {
|
||||
"get": {
|
||||
"summary": "List pets",
|
||||
"operationId": "listPets",
|
||||
"responses": {"200": {"description": "ok"}}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(spec))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
s := NewOpenAPIScraper(&Config{Timeout: 2 * time.Second, UserAgent: "DevourTest"})
|
||||
docs, err := s.Scrape(context.Background(), &Source{Name: "pet", Type: SourceTypeOpenAPI, URL: srv.URL})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected scrape error: %v", err)
|
||||
}
|
||||
if len(docs) < 2 {
|
||||
t.Fatalf("expected at least 2 docs, got %d", len(docs))
|
||||
}
|
||||
foundOp := false
|
||||
for _, d := range docs {
|
||||
if strings.Contains(d.Title, "List pets") {
|
||||
foundOp = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundOp {
|
||||
t.Fatal("expected operation document")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAPIScraperDetectChanges(t *testing.T) {
|
||||
spec := `{"openapi":"3.0.0","info":{"title":"API"},"paths":{}}`
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(spec))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
s := NewOpenAPIScraper(&Config{Timeout: 2 * time.Second, UserAgent: "DevourTest"})
|
||||
src := &Source{Name: "api", Type: SourceTypeOpenAPI, URL: srv.URL}
|
||||
changed, hash1, err := s.DetectChanges(context.Background(), src, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !changed || hash1 == "" {
|
||||
t.Fatalf("expected changed=true and non-empty hash, changed=%v hash=%q", changed, hash1)
|
||||
}
|
||||
|
||||
changed, _, err = s.DetectChanges(context.Background(), src, hash1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if changed {
|
||||
t.Fatal("expected no changes when hash matches")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user