Last active
February 3, 2026 20:18
-
-
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/bae3f1e70e8425f9becd526744088f508d3b977b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : resolve host name | |
| // keywords : scala, dns, resolution, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // 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.4.2" | |
| //> 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