Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/9b3cd638e93069739832c02bbd61bd22 to your computer and use it in GitHub Desktop.
Save dacr/9b3cd638e93069739832c02bbd61bd22 to your computer and use it in GitHub Desktop.
resolve host name / published by https://github.com/dacr/code-examples-manager #35e7ef49-5ef1-48e9-b16c-f6ff78a6df03/f47b70856610d638565a9a5362fc50375621d8bc
// summary : resolve host name
// keywords : scala, dns, resolution, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 35e7ef49-5ef1-48e9-b16c-f6ff78a6df03
// created-on : 2020-07-01T05:11:43Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.1.1"
//> using dep "org.scalatest::scalatest:3.2.10"
// ---------------------
import org.scalatest.*, flatspec.*, matchers.*
import java.net.*
def getAllByName(hostname:String):List[InetAddress] = {
InetAddress.getAllByName(hostname).toList
}
def host(hostname:String):List[String] = {
getAllByName(hostname).map(_.getHostAddress)
}
def host4(hostname:String):List[String] = {
getAllByName(hostname).collect {case ip:Inet4Address => ip.getHostAddress}
}
def host6(hostname:String):List[String] = {
getAllByName(hostname).collect {case ip:Inet6Address => ip.getHostAddress}
}
object UnitTests extends AnyFlatSpec with should.Matchers {
override def suiteName: String = "UnitTests"
"host functions using native (limited) APIs" should "resolve localhost address" in {
host("localhost") should contain only "127.0.0.1"
}
it should "resolve remote host name" in {
host("www.mapland.fr") should contain only("176.9.65.81", "2a01:4f8:150:834b:0:0:0:2")
host4("www.mapland.fr") should contain only("176.9.65.81")
host6("www.mapland.fr") should contain only("2a01:4f8:150:834b:0:0:0:2")
}
}
UnitTests.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment