Skip to content

Commit

Permalink
Add a basic analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Sep 29, 2018
1 parent 1778022 commit f72d318
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
42 changes: 42 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package oaichecker

import (
"errors"
"fmt"
"net/http"

"github.com/go-openapi/analysis"
)

type Analyzer struct {
analyzer *analysis.Spec
}

func NewAnalyzer(specs *Specs) *Analyzer {
if specs == nil {
return &Analyzer{}
}

return &Analyzer{
analyzer: analysis.New(specs.document.OrigSpec()),
}
}

func (t *Analyzer) Analyze(req *http.Request) error {
if t.analyzer == nil {
return errors.New("no specs defined")
}

if req == nil {
return errors.New("no request defined")
}

operation, ok := t.analyzer.OperationFor(req.Method, req.URL.Path)
if !ok {
return errors.New("operation not defined inside the specs")
}

fmt.Printf("operation: %#v\n", operation)

return nil
}
42 changes: 42 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package oaichecker

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_Analyzer_Analyze_with_no_specs(t *testing.T) {
analyzer := NewAnalyzer(nil)

err := analyzer.Analyze(nil)

assert.EqualError(t, err, "no specs defined")
}

func Test_Analyzer_Analyze_with_no_request(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore_minimal.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

err = analyzer.Analyze(nil)

assert.EqualError(t, err, "no request defined")
}

func Test_Analyzer_Analyze_with_request_not_in_specs(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore_minimal.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "invalid/path", nil)
require.NoError(t, err)

err = analyzer.Analyze(req)

assert.EqualError(t, err, "operation not defined inside the specs")
}

0 comments on commit f72d318

Please sign in to comment.