|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | +) |
| 7 | + |
| 8 | +type engine struct { |
| 9 | + Name string |
| 10 | + Imprints []imprint |
| 11 | +} |
| 12 | + |
| 13 | +type imprint struct { |
| 14 | + Query string |
| 15 | + Matcher matcher |
| 16 | +} |
| 17 | + |
| 18 | +// A responseMatcher is a function that takes a response body and returns true if the response matches the engine. |
| 19 | +type matcher func(responseBody *[]byte) bool |
| 20 | + |
| 21 | +// inResponseText returns a responseMatcher that checks if the response body contains any of the given strings. |
| 22 | +func inResponseText(matches []string) matcher { |
| 23 | + return func(responseBody *[]byte) bool { |
| 24 | + for _, match := range matches { |
| 25 | + if bytes.Contains(*responseBody, []byte(match)) { |
| 26 | + return true |
| 27 | + } |
| 28 | + } |
| 29 | + return false |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// inSection returns a responseMatcher that checks if the response body contains any of the given strings in the given section. |
| 34 | +func inSection(section string, matches []string) matcher { |
| 35 | + return func(responseBody *[]byte) bool { |
| 36 | + var reponseBody map[string]interface{} |
| 37 | + json.Unmarshal(*responseBody, &reponseBody) |
| 38 | + content, err := json.Marshal(reponseBody[section]) |
| 39 | + if err != nil { |
| 40 | + return false |
| 41 | + } |
| 42 | + for _, match := range matches { |
| 43 | + if bytes.Contains(content, []byte(match)) { |
| 44 | + return true |
| 45 | + } |
| 46 | + } |
| 47 | + return false |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// hasJsonKey returns a responseMatcher that checks if the response body contains the given key. |
| 52 | +func hasJsonKey(key string) matcher { |
| 53 | + return func(responseBody *[]byte) bool { |
| 54 | + var reponseBody map[string]interface{} |
| 55 | + json.Unmarshal(*responseBody, &reponseBody) |
| 56 | + _, ok := reponseBody[key] |
| 57 | + return ok |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Order is important here, as the first match will be returned. |
| 62 | +// The order has been determined by the usage statistics of the engines. (The higher the usage, the higher the priority.) |
| 63 | +var Engines = []*engine{ |
| 64 | + Apollo, |
| 65 | + AWSAppSync, |
| 66 | + GraphQLGo, |
| 67 | + Ruby, |
| 68 | + GraphQLPHP, |
| 69 | + Graphene, |
| 70 | + Adriane, |
| 71 | + GraphQLGopherGo, |
| 72 | +} |
0 commit comments