This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ScraperSpec.scala
164 lines (110 loc) · 4.65 KB
/
ScraperSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.beachape.metascraper
import java.io.InputStream
import java.nio.ByteBuffer
import java.util
import com.beachape.metascraper.Messages.ScrapeUrl
import com.beachape.metascraper.extractors.{ SchemaFactory, DocsSupport, Schema }
import com.ning.http.client.AsyncHandler.STATE
import com.ning.http.client.cookie.Cookie
import com.ning.http.client.{ HttpResponseHeaders, FluentCaseInsensitiveStringsMap, Response }
import dispatch.{ Uri, Http }
import org.scalatest._
import org.scalatest.concurrent.{ IntegrationPatience, PatienceConfiguration, ScalaFutures }
/**
* Created by Lloyd on 2/15/15.
*/
class ScraperSpec
extends FunSpec
with Matchers
with ScalaFutures
with PatienceConfiguration
with IntegrationPatience
with DocsSupport {
import scala.concurrent.ExecutionContext.Implicits.global
val subject = new Scraper(Http, Seq("http", "https"))
describe("#fetch") {
it("should return a failed future if the url is invalide") {
whenReady(subject.fetch(ScrapeUrl("arg")).failed) { _ shouldBe an[IllegalArgumentException] }
}
it("should return a simple ScrapedData if the url is an image url") {
whenReady(subject.fetch(ScrapeUrl("http://google.com/image.jpg"))) { r =>
r.mainImageUrl shouldBe "http://google.com/image.jpg"
}
}
}
describe("#handleSupportedTypes handler") {
val htmlHandler = subject.handleSupportedTypes(Seq("text/html"))
val htmlRespHeadersMap = {
val m = new FluentCaseInsensitiveStringsMap()
m.add("Content-Type", "text/html")
}
val nonHtmlRespHeadersMap = {
val m = new FluentCaseInsensitiveStringsMap()
m.add("Content-Type", "application/json")
}
it("should create a handler that continues on supported header types") {
val r = htmlHandler.onHeadersReceived(new HttpResponseHeaders() {
def getHeaders: FluentCaseInsensitiveStringsMap = htmlRespHeadersMap
})
r shouldBe STATE.CONTINUE
}
it("should create a handler that aborts on unsupported header types") {
val r = htmlHandler.onHeadersReceived(new HttpResponseHeaders() {
def getHeaders: FluentCaseInsensitiveStringsMap = nonHtmlRespHeadersMap
})
r shouldBe STATE.ABORT
}
}
describe("#extractData") {
val scraper1 = new Schema {
def extractUrl: Option[String] = Some("firstUrl")
def extractTitle: Option[String] = Some("firstTitle")
def extractImages: Seq[String] = Seq("first.jpeg")
def extractDescription: Option[String] = Option("firstDescription")
def extractMainImage: Option[String] = Some("firstMain.jpeg")
}
val scraper2 = new Schema {
def extractUrl: Option[String] = Some("secondUrl")
def extractTitle: Option[String] = Some("secondTitle")
def extractImages: Seq[String] = Seq("second.jpeg")
def extractDescription: Option[String] = Option("secondDescription")
def extractMainImage: Option[String] = Some("second.jpeg")
}
val factory = new SchemaFactory {
val contentTypes = Seq("text/html")
def apply(s: Response): Seq[Schema] = Seq(scraper1, scraper2)
}
def resp(s: String) = new Response {
def getResponseBodyExcerpt(maxLength: Int, charset: String): String = s
def getResponseBodyExcerpt(maxLength: Int): String = s
def getResponseBodyAsByteBuffer: ByteBuffer = ???
def getStatusCode: Int = 200
def getResponseBodyAsBytes: Array[Byte] = ???
def getResponseBodyAsStream: InputStream = ???
def isRedirected: Boolean = ???
def getCookies: util.List[Cookie] = ???
def hasResponseBody: Boolean = ???
def getStatusText: String = "200"
def getHeaders(name: String): util.List[String] = ???
def getHeaders: FluentCaseInsensitiveStringsMap = ???
def hasResponseHeaders: Boolean = ???
def getResponseBody(charset: String): String = s
def getResponseBody: String = s
def getContentType: String = ???
def hasResponseStatus: Boolean = ???
def getUri = ???
def getHeader(name: String): String = ???
}
it("should prioritise Schema scrapers at the beginning of the list") {
val data = subject.extractData(resp(withOgTagsSource.mkString), "hello", Seq(factory), 5)
data.url shouldBe "firstUrl"
data.title shouldBe "firstTitle"
data.description shouldBe "firstDescription"
data.imageUrls shouldBe Seq("first.jpeg", "second.jpeg")
data.mainImageUrl shouldBe "firstMain.jpeg"
// check that images are prioritised
val data2 = subject.extractData(resp(withOgTagsSource.mkString), "hello", Seq(factory), 1)
data2.imageUrls shouldBe Seq("first.jpeg")
}
}
}